View Full Version : Random number feedback please...
MikeC
2007.10.27, 11:24 PM
Does anyone care to give some criticism on this as a random number generator for a number from 1 to100?
- (void) randomGenerate{
srandom( time(NULL));
long myOne,myTwo,myThree,myFour;
int myCount=1;
NSRange pickRange;
pickRange.length = 1;
NSLog(@"Hello");
do {
myOne = (random()>>8) %100000+100000;
myTwo = (random()>>8) %100000+100000;
myThree = (random()>>8) %2+4;
myFour = (random()>>8) %2+4;
NSString *oneString= [NSString stringWithFormat:@"%d",myOne];
NSString *twoString= [NSString stringWithFormat:@"%d",myTwo];
pickRange.location =myThree;
NSString *DigitOneString= [oneString substringWithRange:pickRange];
pickRange.location =myFour;
NSString *DigitTwoString= [twoString substringWithRange:pickRange];
NSString *numString= [NSString stringWithFormat:@"%@%@",DigitOneString,DigitTwoString];
int myNum = [numString intValue]+1;
myCount++;
} while (myCount<11);
}
@end
OneSadCookie
2007.10.28, 12:21 AM
Why so absurdly convoluted?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char *argv[])
{
srandom(time(NULL));
unsigned i;
for (i = 0; i < 10; ++i)
{
int n = random() % 100 + 1;
printf("%d\n", n);
}
}
MikeC
2007.10.28, 11:03 AM
Why so absurdly convoluted?
Yeah, I should have anticipated that one. The reason is because with less convoluted code such as yours I am finding the same number comes twice on ten attempts with suspicious frequency.
1) Is there something about placing the code in the main loop that makes it any more random?
2) I guess what I am asking is whether by making the code absurdly convoluted I am also making it absurdly inefficient or whether on modern hardware this code could be called every second or two with a neglible performance hit.
Thanks
Skorche
2007.10.28, 12:25 PM
1) No? Why would it? The code will run the same no matter where you put it. Computers would be a lot more frustrating otherwise.
2) When you have billions of instructions running per second, you can do pretty much whatever you want once or twice a second with a negligible performance hit. ;)
Lastly, unless you really think that you know of a neat new way to create random numbers, just stick to the beaten path. The C random() function works quite well. Some people also swear by ranrot (Google for it).
AnotherJake
2007.10.28, 12:51 PM
No matter how you cut it, random number generators on computers are *pseudo* random number generators. That is, they aren't truly random. You'll find a pattern no matter what. True randomness requires chaos, and computers just don't do that without special hardware. One thing I've always wondered is why they (engineers) don't make use of the fact that semiconductor junctions are noisy. All they gotta do is put one single noisy transistor on the board and read it with an A/D converter, but whatever, maybe there's a reason why they don't...
Anyway, I like to use the The Mersenne Twister (http://www.qbrundage.com/michaelb/pubs/essays/random_number_generation.html).
Malarkey
2007.10.28, 01:45 PM
(puts on old man cap)
Well, sonny, back in my Commodore 64 days, we used to get truly random numbers by sampling the frequency values of the white noise that the sound chip generated. ;)
AnotherJake
2007.10.28, 02:06 PM
Woah, no kidding! I didn't know that. That noise is *exactly* the same junction noise I'm talking about.
FWIW, I started my programming days back then, but I had a CoCo 1 (you know, the big clunky silver one). I don't recall there being any access to the audio generator, but memory is pretty hazy going that far back...
OneSadCookie
2007.10.28, 04:43 PM
Just make sure you call srandom() exactly once in your program. The more often you call it, the less random your numbers.
And if random() isn't random enough for you, you can always grab an implementation of the Mersenne Twister from the internet, or just read /dev/urandom. Personally, I've always just used (double)rand() / (double)RAND_MAX, or (rand() >> 16) % n, and never had a problem.
MikeC
2007.10.28, 06:02 PM
Great feedback all thanks.
AndyKorth
2007.10.29, 11:28 PM
For more reading, I'd suggest this page:
http://www.random.org/randomness/
That and a lot of intro to statistics courses dispel common misconceptions about probability... For example, if you roll two sixes on a six sided die... what's the probability of getting another six? Yeah... one in six.
skyhawk
2007.10.29, 11:46 PM
For more reading, I'd suggest this page:
http://www.random.org/randomness/
That and a lot of intro to statistics courses dispel common misconceptions about probability... For example, if you roll two sixes on a six sided die... what's the probability of getting another six? Yeah... one in six.
I'm pretty sure it's 0.46% (if they are not mutually exclusive) or rather, .46% chance to get 3 6s in a row.
AndyKorth
2007.10.30, 12:05 AM
To get 3 sixes in a row is 1 in (6*6*6), so yes, about 0.46%.
But the chance to roll a six given that you've already rolled two sixes is 1 in 6.
That is, each die roll is an independent event. Rolling certain numbers on the first roll of a die does not affect the second roll of a die.
http://en.wikipedia.org/wiki/Statistical_independence
AnotherJake
2007.10.30, 01:06 AM
But the chance to roll a six given that you've already rolled two sixes is 1 in 6.
Totally OT:
It depends on who's rolling the dice and how desperate the situation is. I've seen guys roll six sixes all at once in Axis and Allies, pretty much at will, when they needed to roll damage or technology.
I know, I know, I kid... But anyone who's really done a lot of table gaming can tell you they've met some rollers who have clearly bent what probability would have predicted. My buddy John can roll a one in a thousand if the situation is dire enough...
Quick story: One time we were sitting in the living room and my friend John bet me five bucks in front of a bunch of visitors that he could throw his bottle cap over his shoulder without looking and get it in the garbage, twenty feet away. The garbage was well overflowing and so everybody was saying to do it (not to mention it was an impossible throw behind the back). I said no way, and everyone was shocked and laughing at me because I turned the bet down, so some other fool did it. I told them I never bet against John on luck. Well, he did it and they lost the bet, mouths dropped and all.
PowerMacX
2007.10.30, 01:10 AM
Actually, the chance of it being any particular number is inversely proportional to how much money you bet on it ;)
</offtopic>
hokan
2007.10.30, 05:23 PM
You could always read from /dev/random if you want randomness based on hardware noise ( http://www.answers.com/topic/dev-random )
PowerMacX
2007.10.30, 05:32 PM
As stated in that article (emphasis added):
In Unix-like operating systems, /dev/random is a special file that serves as a true random number generator or as a pseudorandom number generator. It allows access to environmental noise collected from device drivers and other sources. Not all operating systems implement the same semantics for /dev/random.
In particular, Mac OS X uses a pseudorandom number generator for /dev/random, the Yarrow algorithm. (http://www.answers.com/topic/yarrow-algorithm)
(also linked from the article)
AnotherJake
2007.10.30, 05:44 PM
Yeah, I was also under the impression that it was pseudo-random, but reading the man page a little more revealed that entropy is added to the pool by the security server. It also mentioned kernel jitter as a source. WTF is kernel jitter?
Najdorf
2007.10.31, 10:03 AM
Totally OT:
It depends on who's rolling the dice and how desperate the situation is. I've seen guys roll six sixes all at once in Axis and Allies, pretty much at will, when they needed to roll damage or technology.
Dude you're lucky you never played with a guy I know that trains one hour a day in throwing 6es (for Risk), and has an estimated probability of getting a 6 in every throw of about 0.4
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.