Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random float number in C

I can't find any solution to generate a random float number in the range of [0,a], where a is some float defined by a user.

I have tried the following, but it doesn't seem to work correctly.

float x=(float)rand()/((float)RAND_MAX/a) 
like image 728
fragon Avatar asked Nov 16 '12 01:11

fragon


People also ask

How do I get a random float number?

Use a random. random() function of a random module to generate a random float number uniformly in the semi-open range [0.0, 1.0) . Note: A random() function can only provide float numbers between 0.1. to 1.0. Us uniform() method to generate a random float number between any two numbers.

How do you generate a random float number in C++?

Using rand() function Another simple, but less preferred solution to generate pseudo-random numbers in C++ is with rand() function. It returns a random number between 0 and RAND_MAX , and can be used to generate floating-point random values in any arbitrary closed interval.

How does C generate random numbers?

1.1.The rand function, declared in stdlib. h, returns a random integer in the range 0 to RAND_MAX (inclusive) every time you call it. On machines using the GNU C library RAND_MAX is equal to INT_MAX or 231-1, but it may be as small as 32767.

Is there a random number generator in C?

C library function - rand()The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.


1 Answers

Try:

float x = (float)rand()/(float)(RAND_MAX/a); 

To understand how this works consider the following.

N = a random value in [0..RAND_MAX] inclusively. 

The above equation (removing the casts for clarity) becomes:

N/(RAND_MAX/a) 

But division by a fraction is the equivalent to multiplying by said fraction's reciprocal, so this is equivalent to:

N * (a/RAND_MAX) 

which can be rewritten as:

a * (N/RAND_MAX) 

Considering N/RAND_MAX is always a floating point value between 0.0 and 1.0, this will generate a value between 0.0 and a.

Alternatively, you can use the following, which effectively does the breakdown I showed above. I actually prefer this simply because it is clearer what is actually going on (to me, anyway):

float x = ((float)rand()/(float)(RAND_MAX)) * a; 

Note: the floating point representation of a must be exact or this will never hit your absolute edge case of a (it will get close). See this article for the gritty details about why.

Sample

#include <stdio.h> #include <stdlib.h> #include <time.h>  int main(int argc, char *argv[]) {     srand((unsigned int)time(NULL));      float a = 5.0;     for (int i=0;i<20;i++)         printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);     return 0; } 

Output

1.625741 3.832026 4.853078 0.687247 0.568085 2.810053 3.561830 3.674827 2.814782 3.047727 3.154944 0.141873 4.464814 0.124696 0.766487 2.349450 2.201889 2.148071 2.624953 2.578719 
like image 98
WhozCraig Avatar answered Sep 25 '22 12:09

WhozCraig