Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Horrible random number generator...

Horrible random number generator...
Thread Tools
Fresh-Faced Recruit
Join Date: May 2001
Status: Offline
Reply With Quote
Oct 14, 2001, 04:07 AM
 
Hi,

I was wondering if anyone else has noticed how aweful the random number generator in MacOS X is? I wrote a simple OpenGL application that builds a randomly created 2D maze, on Linux and Windows the maze is as random as one would expect. However, on MacOS X the maze has a distinct "stair-step" pattern, and is almost as far from random as one could get.

I am seeding srand(); with the system time, and then using rand(); during my data building.

Any ideas how to work around this?

Thanks,

Nathan
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Oct 14, 2001, 07:38 AM
 
Yes, rand() is a yucky thing. I haven't used random(), but you may want to try that. Or else, there are plenty short random number generators on the web (the 'Mersenne Twister' is the one I remember from a thread many months back).
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Fresh-Faced Recruit
Join Date: Jun 1999
Location: Belo Horizonte, MG, Brazil
Status: Offline
Reply With Quote
Oct 14, 2001, 09:03 AM
 
The Apple team said they didn't provide a /dev/random entry in previous versions of Darwin because they wanted to make sure they had the best implementation.

Now they provide one, and you say it isn't any good. Weird...

Unless you are not using /dev/random. Does rand() rely on /dev/random ?

[ 10-14-2001: Message edited by: Elifarley ]
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Oct 14, 2001, 02:37 PM
 
No, he'll be using the old and crappy rand() that doesn't work too well. I suggest using /dev/random, but I don't know how to do that other than opening up a pipe to /dev/random and then reading some bytes from it, but there must be a cleaner way of doing it.
     
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status: Offline
Reply With Quote
Oct 14, 2001, 02:38 PM
 
Use random() and not rand(). From the random() man page:

The random()/ srandom() have (almost) the same calling sequence and initialization properties as rand(3)/ srand(3). The difference is that rand produces a much less random sequence -- in fact, the low dozen bits generated by rand go through a cyclic pattern.
Matt
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Oct 15, 2001, 12:48 AM
 
As everyone else had said, rand() is an awful generator, really only provided for back compatibility. random() is pretty decent, so use that instead. If you really need portability, or the quality of the PRNG is of high importance to you, then I'd go out and find a free implementation of one out there. Mersenne Twister is GPL'ed and quite good; there are other public domain/BSD licenced ones out there as well if the GPL is a problem.

/dev/random gives out truly random numbers (based on lots of changing kernel state) and not pseudo-random, and has nothing to do with rand() or random(). However in virtually all circumstances you don't want to use it as the main random number generator for performance reasons. If you need an unpredictable seed (usually in cryptographic apps), then it's a really good idea to seed your PRNG (i.e. srandom()) with bits from /dev/random rather than using the system time.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Oct 15, 2001, 02:10 AM
 
Uh oh. I just did an entire Comp Sci assignment using rand() for sequencing population genetics. But most of the stuff was compiled on a Solaris server and the results seemed pretty random. Oh well. I won't use it again.
     
Dedicated MacNNer
Join Date: Mar 2001
Location: Iowa City, IA
Status: Offline
Reply With Quote
Oct 15, 2001, 02:59 PM
 
rand() isn't always bad, it's just that there are no guarantees about its quality.

I wouldn't be surprised if Solaris and IRIX and the like had relatively robust implementations.
James

"I grew up. Then I got better." - Sea Wasp
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Oct 18, 2001, 03:11 PM
 
As usual, the comp.lang.C FAQ has something to say on this:
http://www.eskimo.com/~scs/C-faq/q13.16.html

I use rand as explained in the page, not random, and it works fine for my needs.

-Peter
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:22 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2