 |
 |
random numbers...
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
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
|
|
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.
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2000
Status:
Offline
|
|
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. 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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