Date: Tue, 18 Feb 2003 16:04:57 -0800 (PST) From: Paul Herman <pherman@frenchfries.net> To: FreeBSD Hackers <hackers@freebsd.org> Subject: arc4random() range Message-ID: <20030218153651.A240-100000@mammoth.eat.frenchfries.net>
next in thread | raw e-mail | index | archive | help
Hi,
...a potential quick commit for someone. :-)
What's the concesus that arc4random() should be a drop-in
replacement for rand()/random()?  Consider the following that
caclulates the average of a bunch of random numbers on [0.0, 1.0]:
  bash$ cat rand.c
  #include <stdlib.h>
  int i;
  double avg;
  #define ITERATIONS 1000000
  #define FIND_AVG(func)  do { \
                for (i=0, avg = 0.0; i<ITERATIONS; i++)                 \
                        avg += (double)func()/(double)RAND_MAX;         \
                printf("avg:\t%f\n", avg/ITERATIONS);                   \
                } while (0)
  int main() {
        FIND_AVG(rand);
        FIND_AVG(random);
        FIND_AVG(arc4random);
        return 0;
  }
  bash$ cc rand.c && ./a.out
  avg:    0.500404
  avg:    0.500007
  avg:    1.001370	<--- !!!!!!
  bash$ uname -r
  4.7-STABLE
  bash$
Is is just understood that arc4random() doesn't obey RAND_MAX, or
should it?  Whatever the answer is, perhaps the manpage should
state what the range is like the manpages for rand(3) and random(3)
do.
-Paul.
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030218153651.A240-100000>
