I can't figure out how to do random numbers!!!! I want to make a random # between 1 and 100, but It makes the number whatever it wants! How do I set a range, and make it so the random number is inputed into a variable?
I can't figure out how to do random numbers!!!! I want to make a random # between 1 and 100, but It makes the number whatever it wants! How do I set a range, and make it so the random number is inputed into a variable?
FAQ!!! Nobody reads it, so we will keep seeing this question.
-Prelude
My best code is written with the delete key.
I read it, but I still don't understand them.
Thisis something I would like to know also, if someone could be so kind.
This is a random number generator I made because I was not satisfied by the one provided by Standard C/C++.
This one is automatically seeded after you declare the class object within your code and you simply have to call the random_tololot() function.
You can just hotwire the source like so:
I'm assume you know how to handle simple classes. If you do, then just fire away!PHP Code:
#include "random.cpp"
int main(void)
{
RANDOM go;
int num;
/*...some other code...*/
num = go.random_tololot(1, 1000);
/*...some more code...*/
return 0;
}
All men are created equal. But some are more equal than others.
Visit me at http://www.angelfire.com/my/billholm
Hi,
Another option --
-GeraldCode:#include <stdio.h> #include <stdlib.h> int main() { int i; int minumum = 1; int maximum = 100; srand(time(NULL)); /* seed randomizer with current time makes rand() rather more unique on each call*/ i = minumum + (int)(rand()%maximum); /* simple but not recommended */ /* i = minumum + (int)((double)maximum*rand()(RAND_MAX+1.0)); is better */ printf("\ni is %d\n", i); }
-------------------------
Gerald.
beg_g, I believe you must take the modulus as (max-min+1) to get the number in range. Otherwise you could have numbers outside the range.
Ex.
min = 98; max = 100; rand is 44
->rand % max = 44
->min + (rand % max) = 142
or
rand % (max-min+1) = 2
min + rand %(max-min+1) = 100
--Chorus
PS: depending on the random number generator, I believe you can lose "randomness" by taking modulus. You might be better off scaling the value into the appropriate range and truncating the mantissa.