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

random numbers...
Thread Tools
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 13, 2001, 08:14 PM
 
So, I'm using:
Code:
int i = 0; int result = 0; srand(time(NULL)); for(i=0;i<MAX_DICE;i++) { result = rand()%MAX_DICE; [dice[i] setIntValue: ++result]; // ++result for 1-6 instead of 0-5 }
Is there a better way? It seems like what I get could be a little more random.

thanks in advance!

[typo]

[This message has been edited by IamBob (edited 02-13-2001).]
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Feb 14, 2001, 01:58 AM
 
Use srandom()/random() instead of srand()/rand(). *Much* better generator.

Other than that, dunno. You could try to seed the generator a bit more unpredictably, say maybe srandom(time(NULL) * getpid()), though I'm not sure how much that would help in your case.

Another thing to point out is that it may not be a good idea to keep re-seeding the generator -- it may seem this may make things "more random" but often it doesn't. Maybe try just seeding it once at the start of the program.

If MacOS X ever gets a /dev/[u]random, that would likely be the best way (though probably not the fastest). The Public Beta doesn't have it though.

     
Forum Regular
Join Date: Dec 2000
Status: Offline
Reply With Quote
Feb 14, 2001, 09:40 AM
 
There was a patch posted on http://www.darwinfo.org/ a while ago that would give you a /dev/random
I didn't try to use it, but it might be interesting.
     
IamBob  (op)
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 14, 2001, 11:58 AM
 
I just tried what you suggested. Works great! Now I get to play around with it and see what I come up with!

Thanks!
     
Mac Enthusiast
Join Date: Sep 2000
Location: Cupertino, CA
Status: Offline
Reply With Quote
Feb 15, 2001, 02:40 PM
 
Originally posted by Mniot:
There was a patch posted on http://www.darwinfo.org/ a while ago that would give you a /dev/random
I didn't try to use it, but it might be interesting.
As the person who wrote /dev/random (well, I took a lot of it from FreeBSD) I can tell you that it will probably make it into OS X mainstream, but not until after 1.0. The code on my page has some potential stream corruption of the numbers that might allow for attacks, but I think the SMP races are clean. If you have any comments about quality random numbers in OS X please let me know.

Louis
Louis Gerbarg
Darwin Developer
These are my views, and not the views of my employer.
     
IamBob  (op)
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 16, 2001, 11:02 AM
 
I ended up using a little of both.

Something like:
Code:
- (void)init { srand(time(NULL) * MAX_DICE); } - (void)reSeed { srandom(time(NULL) * rand()%MAX_DICE); } - (void)roll { int i=0; int result = 0; for(i=0;i<MAX_DICE;i++) { result = random()%MAX_DICE; [dice[i] setIntValue: ++result]; } }
Then I reseed every so often to get a little extra randomness out of it. I wrote up a test that logs what I get with MAX_DICE equalling [input] and it looks pretty good.

Anyway, for what I'm doing this is plenty good enough. Check out what this went into here.
     
   
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 12:23 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