# Thread: Random number in range generation.

1. ## Random number in range generation.

Hi,
I've seen various methods for generating a random number within a range around the internet and in books, but some are very roundabout and I'm wondering if someone could provide me with the simplest method for generating a random number in a certain range. My program will have to do this many times, and so I would like as much succinctness as possible.

An instance of what would need to be calculated:

/* I would like floating point random numbers with three decimal places.
The range should be set by another place in the program, and so I need variables as the values of RangeMin & RangeMax */

float RangeMin;
float RangeMax;

Rand_Num1 = rand(....in the range of Min to Max...)
etc

Thanks.

2. Here's a good article on the ins and outs of using rand().

http://www.eternallyconfuzzled.com/a..._art_rand.aspx

gg

3. As you can tell by reading the excellent article on the linked site, random number generation is not about being succinct, it's about the *quality* of the random numbers, that you need for your program.

For a simple program, where no serious consequences are afoot, the simple ways will do fine. Just be aware that they are simple, and rudely different than true random numbers.

4. Is there something wrong with this implementation?
It compiles, but when it runs, the program ends without displaying anything.

THANKS!

Code:
#include <stdio.h>
#include <stdlib.h>

float Tval_v_0_maxZ;

float Tval_force_v_0;

float f_v_0_x;
float f_v_0_y;
float f_v_0_z;

float r_v_0_x;
float r_v_0_y;
float r_v_0_z;

// Xforce = Tval(Xreal);
// Yforce = Tval(Yreal);
// Zforce = Tval(Zreal);

int main (void) {

r_v_0_x = 10;
r_v_0_y = 15;
r_v_0_z = -40;

/* case p */

Tval_v_0_maxZ = -20 / r_v_0_z;

/* Vertex 0 */
do
Tval_force_v_0 = rand();
while (Tval_force_v_0 > Tval_v_0_maxZ);

f_v_0_x = Tval_force_v_0 * r_v_0_x;
f_v_0_y = Tval_force_v_0 * r_v_0_y;
f_v_0_z = Tval_force_v_0 * r_v_0_z;

printf("Force Vertex 0 is (&#37;f,%f,%f) using Tval_force_v_0 of %f \n",f_v_0_x,f_v_0_y,f_v_0_z,Tval_force_v_0);
return(0);
}

5. It is very strange. It simply will not run, but it compiles. This happened with another script of mine, which I know was working the previous night.

6. By process of elimination, I found that only my rand function was causing problems.

7. Could anyone please break down the necessary parts of a program that includes a random number generator? What I mean is, I'm finding it hard to decipher what goes before "int main (void) {"
and such. I want to generate many random floating point numbers in my program (with greater-than/less-than ranges). These random numbers will be used in the main body of the program and the ranges generated for them will be provided by user inputs.

Thanks. Randomness is tough.

8. Originally Posted by Codeplug
Here's a good article on the ins and outs of using rand().

http://www.eternallyconfuzzled.com/a..._art_rand.aspx

gg
Let us know if there's anything in there you don't understand.

gg

9. I guess I just don't understand the parameters for providing a range in:

[CODE]
unsigned time_seed()
{
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t i;

for ( i = 0; i < sizeof now; i++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[i];

return seed;
}

srand ( time_seed() );
[/CODE}

Some of the other examples on that site have 'M' and 'N' value, but I'm unclear about how to do that in the above version.
Particularly if I want floating integers for a range of negative numbers. (though I do understand how to put floating integers into the earlier examples.)

What do you put instead of:
Code:
int r = M + uniform_deviate ( rand() ) * ( N - M );
from the earlier example?

Thanks.

10. Some of the other examples on that site have 'M' and 'N' value, but I'm unclear about how to do that in the above version.
You want a random float in a range using the code from that link? I'm not sure what all that stuff is for, but this works and uses code I pinched from the page. Don't ask me how it works though.
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double uniform_deviate ( int seed )
{
return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
}

unsigned time_seed()
{
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t i;

for ( i = 0; i < sizeof now; i++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[i];

return seed;
}

int main(void)
{
double m, n;
int i;

srand(time_seed());

scanf("%lf%lf", &m, &n);

for ( i = 0; i < 25; ++i )
{
double r = m + uniform_deviate(rand()) * (n - m);

printf("[%g..%g): %f\n", m, n, r);
}

return 0;
}

11. Hi, thanks.
That works. But I'm noticing some very recognizable trends in the outputs: in the generation of only 11 numbers, I had 2 that were the same.
That website hyped this as a great solution, but I'm not totally seeing it.

At the very least, I can't have it creating the same four decimal point float in a range of 400.
Are there any other methods that might aid this?

12. Random numbers are likely to repeat now and again. How often depends on the implementation of the random number generator.

This version uses whatever implementation is in the C runtime library - which may not be very good in your particular situation.

I copied the above code, and in the 25 numbers listed, I couldn't see a duplicate for the range [0..400) that you get for inputting 0 400 to the app.

--
Mats

13. What M and N are you using?

gg

14. Originally Posted by hebali
At the very least, I can't have it creating the same four decimal point float in a range of 400.
Are there any other methods that might aid this?
If that's really an issue, then you don't want random numbers -- or more to the point, you need a random shuffle. My back of the envelope calculation says that for what you gave (4000000 possible numbers), in 11 trials you'll get a repeated number 1 out of 50000 times.

15. My M and N are negative number:
M = -10, N = -400
how does this affect the equation?