1 /* Copyright (c) 2016 Joel Sherrill <joel@rtems.org> */
2 /*
3 FUNCTION
4 <<random>>, <<srandom>>---pseudo-random numbers
5 
6 INDEX
7 	random
8 INDEX
9 	srandom
10 
11 SYNOPSIS
12 	#define _XOPEN_SOURCE 500
13 	#include <stdlib.h>
14 	long int random(void);
15 	void srandom(unsigned int <[seed]>);
16 
17 
18 
19 DESCRIPTION
20 <<random>> returns a different integer each time it is called; each
21 integer is chosen by an algorithm designed to be unpredictable, so
22 that you can use <<random>> when you require a random number.
23 The algorithm depends on a static variable called the ``random seed'';
24 starting with a given value of the random seed always produces the
25 same sequence of numbers in successive calls to <<random>>.
26 
27 You can set the random seed using <<srandom>>; it does nothing beyond
28 storing its argument in the static variable used by <<rand>>.  You can
29 exploit this to make the pseudo-random sequence less predictable, if
30 you wish, by using some other unpredictable value (often the least
31 significant parts of a time-varying value) as the random seed before
32 beginning a sequence of calls to <<rand>>; or, if you wish to ensure
33 (for example, while debugging) that successive runs of your program
34 use the same ``random'' numbers, you can use <<srandom>> to set the same
35 random seed at the outset.
36 
37 RETURNS
38 <<random>> returns the next pseudo-random integer in sequence; it is a
39 number between <<0>> and <<RAND_MAX>> (inclusive).
40 
41 <<srandom>> does not return a result.
42 
43 NOTES
44 <<random>> and <<srandom>> are unsafe for multi-threaded applications.
45 
46 _XOPEN_SOURCE may be any value >= 500.
47 
48 PORTABILITY
49 <<random>> is required by XSI. This implementation uses the same
50 algorithm as <<rand>>.
51 
52 <<random>> requires no supporting OS subroutines.
53 */
54 
55 #ifndef _REENT_ONLY
56 
57 #include <stdlib.h>
58 
59 extern NEWLIB_THREAD_LOCAL long long _rand_next;
60 
61 void
srandom(unsigned int seed)62 srandom (unsigned int seed)
63 {
64 	_rand_next = seed;
65 }
66 
67 #endif /* _REENT_ONLY */
68