Random number generating...

Started by
23 comments, last by IronMan9675 22 years, 8 months ago
quote:Original post by Anonymous Poster
Strike three! I'm out! Didn't understand a word of that sentence above.

Why wouldn't the (rand() / RAND_MAX) * anything work?


rand () returns an int between 0 and RAND_MAX-1, inclusive.
RAND_MAX is an integer.
Any integer divided by an integer that is greater than it produces zeros.
That statement will only produce zeros.

I just didn't want to come right out and say it, so I tried to insert a little information theory humor. Entropy is a way of measuring the amount of information in a process (the more random, the more information). I was going to say a zero-mean absolutely deterministic process, but I thought that would have been giving it away.

Only took a couple of days for someone to notice. *sigh*

EDIT: rand actually goes up to RAND_MAX, not RAND_MAX-1. So if you use the above algorithm, you will get a value of (multiplier) 1 time in every 32768 times with a uniform rand function, and 0 the rest of the time. Therefore it's not zero-mean, but it still has very low entropy. Sorry for the mix-up.

Edited by - Stoffel on August 15, 2001 4:31:51 PM
Advertisement
quote:Original post by Anonymous Poster
Why wouldn't the (rand() / RAND_MAX) * anything work?
Because dividing a number by another number larger than it yields a value between 0 and 1. And storing numbers like that in an int is just gonna give you 0. (There is technically an exception: when rand() returns RAND_MAX you'll get 1 instead of 0. That will occur about once in every 4 billion occurences or something like that. Edit #1: And before Stoffel says I'm wrong, MSDN says "The constant RAND_MAX is the maximum value that can be returned by the rand function." So, if I'm wrong, MSDN is wrong. Wouldn't be the first time. Edit #2: Heh... we were editing our messages at the same time. For all intents and purposes, you can pretty much assume it'll return zero all the time.)


Edited by - Kylotan on August 15, 2001 4:32:32 PM

Edited by - Kylotan on August 15, 2001 4:34:52 PM
Edit: LMAO
Edit: ROFL
Edit: that was pretty funny.

Too bad there aren''t mutexes on web posts. =)
After all the studying I did to solve concurrent programming problems, not once did I learn how to solve the issue of concurrent programmers
Ok. Let''s make it really simple.

double random_number = rand();double denom = RAND_MAX;double result = (random_number / denom) * 50.0;cout << "Your random number (0-50): " << result << "\n"; 


And if you want just the integer part, you should round the number.

This topic is closed to new replies.

Advertisement