Unix Technical Forum

ksh's ${RANDOM}

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 ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > mailing.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-18-2008, 07:00 AM
Paul de Weerd
 
Posts: n/a
Default ksh's ${RANDOM}

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/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com