This is a discussion on ksh's ${RANDOM} within the mailing.openbsd.tech forums, part of the OpenBSD category; --> List, After some discussion with a friend, it turns out that the default ksh as shipped with OpenBSD has ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| List, After some discussion with a friend, it turns out that the default ksh as shipped with OpenBSD has a ${RANDOM} which isn't very random : $ for d in `jot 10`; do echo -n ${RANDOM}\ ; done; echo 24831 11980 17685 42 3611 27576 7569 29942 28919 2916 If you're not that fast on the numbers, check this out : $ for d in `jot 30`; do echo -n $((${RANDOM} % 2))\ ; done; echo 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 This appears to be coming from /usr/src/bin/ksh/var.c - it uses the rand() function. From rand(3) : rand, srand - bad random number generator and DESCRIPTION These interfaces are obsoleted by random(3). So this might not be the best choice. I patched up ksh's var.c to use random() in stead of rand(). Then I noticed revision 1.17 in CVS, this has a similar (better, it has the option to revert to the rand() behaviour by seeding the RNG) patch so I figured I should be safe. However, on my newly installed machine (snapshot from 20040601), I get the same behaviour with ksh's $RANDOM producing alternating odd and even numbers. .... OK .. after a lot of debugging (newbie programmer - sorry) I found the problem. How about the following patch : Index: main.c ================================================== ================= RCS file: /cvs/src/bin/ksh/main.c,v retrieving revision 1.26 diff -u -r1.26 main.c --- main.c 8 Jan 2004 05:43:14 -0000 1.26 +++ main.c 7 Jun 2004 22:52:12 -0000 @@ -254,9 +254,6 @@ } ppid = getppid(); setint(global("PPID"), (long) ppid); -#ifdef KSH - setint(global("RANDOM"), (long) (time((time_t *)0) * kshpid * ppid)); -#endif /* KSH */ /* setstr can't fail here */ setstr(global(version_param), ksh_version, KSH_RETURN_ERROR); Revision 1.17 of var.c introduces the option to restore the old behaviour by seeding the RNG. Unfortunately, this seeding is done in main.c. The patch above removes this standard seeding (I don't think it's required with arc4random). No changes to the manpage are required. Comments ? Cheers, Paul 'WEiRD' de Weerd -- >++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+ +++++++++++>-]<.>++[<------------>-]<+.--------------.[-] http://www.weirdnet.nl/ |