]>
diplodocus.org Git - nmh/blob - sbr/m_rand.c
2 * m_rand -- provides pseudorandom bytes
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
9 #include <stdlib.h> /* for abs(), srand(), rand(), arc4random() */
10 #include <stdio.h> /* for fopen(), fread(), fclose() */
11 #include <unistd.h> /* for getpid() */
12 #include <time.h> /* for time() */
17 static int seeded
= 0;
22 m_rand (unsigned char *buf
, size_t n
) {
28 if ((devurandom
= fopen ("/dev/urandom", "r"))) {
29 if (fread (&seed
, sizeof (seed
), 1, devurandom
) == 1) seeded
= 1;
34 /* This seed calculation is from Helmut G. Katzgraber, "Random
35 Numbers in Scientific Computing: An Introduction",
36 arXiv:1005.4117v1 [physics.comp-ph], 22 May 2010, p. 19.
37 time() and getpid() shouldn't fail on POSIX platforms. */
38 seed
= abs ((int) ((time (0) * 181 * ((getpid ()-83) * 359)) % 104729));
47 unsigned char *rndp
= (unsigned char *) &rnd
;
50 for (i
= 0; i
< sizeof rnd
&& n
> 0; ++i
, --n
) {
55 arc4random_buf(buf
, n
);