summaryrefslogtreecommitdiffstats
path: root/newlib/libc/stdlib/random.c
diff options
context:
space:
mode:
authorJoel Sherrill <joel@rtems.org>2016-03-22 17:50:48 -0500
committerJoel Sherrill <joel@rtems.org>2016-03-28 22:39:50 -0500
commitecf453f9635fb278cff4d4bae21a1e249313b817 (patch)
tree691716a58754527b45dd04f53f36731b12596899 /newlib/libc/stdlib/random.c
parenta0c477242996403344a0b3c46e6d4645a3fa672c (diff)
downloadcygnal-ecf453f9635fb278cff4d4bae21a1e249313b817.tar.gz
cygnal-ecf453f9635fb278cff4d4bae21a1e249313b817.tar.bz2
cygnal-ecf453f9635fb278cff4d4bae21a1e249313b817.zip
Add simple versions of random() and srandom()
Prototypes also added for initstate() and setstate() but they were not implemented in the shared newlib code. * newlib/libc/include/cygwin/stdlib.h: Prototypes added. * winsup/cygwin/include/cygwin/stdlib.h: Prototypes removed. * newlib/libc/stdlib/random.c: New file. * newlib/libc/machine/epiphany/machine/stdlib.h: Removed * newlib/libc/stdlib/Makefile.am: Added random.c. * newlib/libc/stdlib/stdlib.tex: Added random.def. * newlib/libc/stdlib/Makefile.in: Regenerated.
Diffstat (limited to 'newlib/libc/stdlib/random.c')
-rw-r--r--newlib/libc/stdlib/random.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/newlib/libc/stdlib/random.c b/newlib/libc/stdlib/random.c
new file mode 100644
index 000000000..de132f363
--- /dev/null
+++ b/newlib/libc/stdlib/random.c
@@ -0,0 +1,82 @@
+/*
+FUNCTION
+<<random>>, <<srandom>>---pseudo-random numbers
+
+INDEX
+ random
+INDEX
+ srandom
+
+ANSI_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>
+#include <reent.h>
+
+void
+_DEFUN (srandom, (seed), unsigned int seed)
+{
+ struct _reent *reent = _REENT;
+
+ _REENT_CHECK_RAND48(reent);
+ _REENT_RAND_NEXT(reent) = seed;
+}
+
+long int
+_DEFUN_VOID (random)
+{
+ struct _reent *reent = _REENT;
+
+ /* This multiplier was obtained from Knuth, D.E., "The Art of
+ Computer Programming," Vol 2, Seminumerical Algorithms, Third
+ Edition, Addison-Wesley, 1998, p. 106 (line 26) & p. 108 */
+ _REENT_CHECK_RAND48(reent);
+ _REENT_RAND_NEXT(reent) =
+ _REENT_RAND_NEXT(reent) * __extension__ 6364136223846793005LL + 1;
+ return (long int)((_REENT_RAND_NEXT(reent) >> 32) & RAND_MAX);
+}
+
+#endif /* _REENT_ONLY */