is this simply done by:
int varible=random(x)
is this right? If not could someone help me?
Printable View
is this simply done by:
int varible=random(x)
is this right? If not could someone help me?
read faq.
Us this code for random integer.
-----
int num = 0;
num = static_cast<int>((((static_cast<double> (rand())) / (static_cast<double> (RAND_MAX + 1))) * 256 + 0));
-----
In the code above, the "256" is the maximum limit you want the integer to be. The 0 is the minimum limit. For example, let say you want to generate an integer from 99 to 108. You will this this code:
-----
// static_cast<int>((((static_cast<double> (rand())) / (static_cast<double> (RAND_MAX + 1))) * 108 + 99));
-----
Kuphryn
Why all the static casts in there?
Although you might be able to use previous example I would strongly advice to use the code from the FAQ instead.
The code I posted is very, very close to being random. In his book, Bjarne Stroustrup made a strong point of not using rand() because it is not truly random.
Kuphryn
thanks
the way most easy to use the random is
example:
int var1;
var=rand()%1+100; // 1 to 100 numbers