File: srandom.c

package info (click to toggle)
picolibc 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 31,616 kB
  • sloc: ansic: 312,308; asm: 22,739; perl: 2,414; sh: 1,619; python: 1,019; pascal: 329; exp: 287; makefile: 164; xml: 40; cpp: 10
file content (67 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Copyright (c) 2016 Joel Sherrill <joel@rtems.org> */
/*
FUNCTION
<<random>>, <<srandom>>---pseudo-random numbers

INDEX
	random
INDEX
	srandom

SYNOPSIS
	#define _XOPEN_SOURCE 500
	#include <stdlib.h>
	long int random(void);
	void srandom(unsigned int <[seed]>);



DESCRIPTION
<<random>> returns a different integer each time it is called; each
integer is chosen by an algorithm designed to be unpredictable, so
that you can use <<random>> when you require a random number.
The algorithm depends on a static variable called the ``random seed'';
starting with a given value of the random seed always produces the
same sequence of numbers in successive calls to <<random>>.

You can set the random seed using <<srandom>>; it does nothing beyond
storing its argument in the static variable used by <<rand>>.  You can
exploit this to make the pseudo-random sequence less predictable, if
you wish, by using some other unpredictable value (often the least
significant parts of a time-varying value) as the random seed before
beginning a sequence of calls to <<rand>>; or, if you wish to ensure
(for example, while debugging) that successive runs of your program
use the same ``random'' numbers, you can use <<srandom>> to set the same
random seed at the outset.

RETURNS
<<random>> returns the next pseudo-random integer in sequence; it is a
number between <<0>> and <<RAND_MAX>> (inclusive).

<<srandom>> does not return a result.

NOTES
<<random>> and <<srandom>> are unsafe for multi-threaded applications.

_XOPEN_SOURCE may be any value >= 500.

PORTABILITY
<<random>> is required by XSI. This implementation uses the same
algorithm as <<rand>>.

<<random>> requires no supporting OS subroutines.
*/

#ifndef _REENT_ONLY

#include <stdlib.h>

extern NEWLIB_THREAD_LOCAL long long _rand_next;

void
srandom (unsigned int seed)
{
	_rand_next = seed;
}

#endif /* _REENT_ONLY */