 |
 |
Horrible random number generator...
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2001
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|