Randomic functions ?
-
I would like to use a function that returns a integer value between two values. I met the rand() function, but I need a value in a range. []'s Cristiano.
(rand()/RAND_MAX)*5
for [between 0-5](rand()/RAND_MAX)*n
for [between 0-n]
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
(rand()/RAND_MAX)*5
for [between 0-5](rand()/RAND_MAX)*n
for [between 0-n]
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
(rand()/RAND_MAX)*5
for [between 0-5](rand()/RAND_MAX)*n
for [between 0-n]
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
The following will print 100 random values between 120 and 320 int getRange (int low, int high) { return low + (rand () % (high - low)); } int main (int argc, char *argv[]) { srand (time (NULL)); for (int x = 0; x < 100; x++) printf ("Random Value %d = %d\n", x, getRange (120, 320)); return 0; }
-
(rand()/RAND_MAX)*5
for [between 0-5](rand()/RAND_MAX)*n
for [between 0-n]
Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]
-
Almost... The problem is: int rand(void); so it should be: rand()*n/RAND_MAX or ((double)rand()/RAND_MAX)*n Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.
This is not correct. The expression yields a value between 0 and n, but the probability of n is miniscule (1/(
RAND_MAX
+1), to be precise), which is not the probability for the other values. The following return a random value between 0 and n with uniform distribution:int randn(int n)
{
int r;
while((r=rand())==RAND_MAX);
return ((double)r/RAND_MAX)*(n+1);
}Joaquín M López Muñoz Telefónica, Investigación y Desarrollo