OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdlib/] [rand.c] - Blame information for rev 258

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
FUNCTION
3
<<rand>>, <<srand>>---pseudo-random numbers
4
 
5
INDEX
6
        rand
7
INDEX
8
        srand
9
INDEX
10
        rand_r
11
 
12
ANSI_SYNOPSIS
13
        #include <stdlib.h>
14
        int rand(void);
15
        void srand(unsigned int <[seed]>);
16
        int rand_r(unsigned int *<[seed]>);
17
 
18
TRAD_SYNOPSIS
19
        #include <stdlib.h>
20
        int rand();
21
 
22
        void srand(<[seed]>)
23
        unsigned int <[seed]>;
24
 
25
        void rand_r(<[seed]>)
26
        unsigned int *<[seed]>;
27
 
28
 
29
DESCRIPTION
30
<<rand>> returns a different integer each time it is called; each
31
integer is chosen by an algorithm designed to be unpredictable, so
32
that you can use <<rand>> when you require a random number.
33
The algorithm depends on a static variable called the ``random seed'';
34
starting with a given value of the random seed always produces the
35
same sequence of numbers in successive calls to <<rand>>.
36
 
37
You can set the random seed using <<srand>>; it does nothing beyond
38
storing its argument in the static variable used by <<rand>>.  You can
39
exploit this to make the pseudo-random sequence less predictable, if
40
you wish, by using some other unpredictable value (often the least
41
significant parts of a time-varying value) as the random seed before
42
beginning a sequence of calls to <<rand>>; or, if you wish to ensure
43
(for example, while debugging) that successive runs of your program
44
use the same ``random'' numbers, you can use <<srand>> to set the same
45
random seed at the outset.
46
 
47
RETURNS
48
<<rand>> returns the next pseudo-random integer in sequence; it is a
49
number between <<0>> and <<RAND_MAX>> (inclusive).
50
 
51
<<srand>> does not return a result.
52
 
53
NOTES
54
<<rand>> and <<srand>> are unsafe for multi-threaded applications.
55
<<rand_r>> is thread-safe and should be used instead.
56
 
57
 
58
PORTABILITY
59
<<rand>> is required by ANSI, but the algorithm for pseudo-random
60
number generation is not specified; therefore, even if you use
61
the same random seed, you cannot expect the same sequence of results
62
on two different systems.
63
 
64
<<rand>> requires no supporting OS subroutines.
65
*/
66
 
67
#ifndef _REENT_ONLY
68
 
69
#include <stdlib.h>
70
#include <reent.h>
71
 
72
void
73
_DEFUN (srand, (seed), unsigned int seed)
74
{
75
  _REENT_CHECK_RAND48(_REENT);
76
  _REENT_RAND_NEXT(_REENT) = seed;
77
}
78
 
79
int
80
_DEFUN_VOID (rand)
81
{
82
  /* This multiplier was obtained from Knuth, D.E., "The Art of
83
     Computer Programming," Vol 2, Seminumerical Algorithms, Third
84
     Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
85
  _REENT_CHECK_RAND48(_REENT);
86
  _REENT_RAND_NEXT(_REENT) =
87
     _REENT_RAND_NEXT(_REENT) * __extension__ 6364136223846793005LL + 1;
88
  return (int)((_REENT_RAND_NEXT(_REENT) >> 32) & RAND_MAX);
89
}
90
 
91
#endif /* _REENT_ONLY */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.