# randomizing a variable

• 05-30-2002
Octorok101
randomizing a variable
is this simply done by:

int varible=random(x)

is this right? If not could someone help me?
• 05-30-2002
|deep|
• 05-30-2002
kuphryn
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
• 05-30-2002
Unregistered
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.
• 05-30-2002
kuphryn
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
• 05-30-2002
Octorok101
thanks
• 05-31-2002
christian
random
the way most easy to use the random is

example:

int var1;

var=rand()%1+100; // 1 to 100 numbers