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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [stdlib/] [rand.c] - Blame information for rev 56

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

Line No. Rev Author Line
1 39 lampret
/*
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-thread applications.
55
<<rand_r>> is MT-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 56 joel
#ifndef _REENT_ONLY
68
 
69 39 lampret
#include <stdlib.h>
70 56 joel
#include <reent.h>
71 39 lampret
 
72
void
73
_DEFUN (srand, (seed), unsigned int seed)
74
{
75 56 joel
        _REENT->_new._reent._rand_next = seed;
76 39 lampret
}
77
 
78
int
79
_DEFUN_VOID (rand)
80
{
81 56 joel
        return ((_REENT->_new._reent._rand_next =
82
                 _REENT->_new._reent._rand_next * 1103515245 + 12345 )
83
                & RAND_MAX );
84 39 lampret
}
85 56 joel
 
86
#endif /* _REENT_ONLY */

powered by: WebSVN 2.1.0

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