Question #6671   Submitted by Answiki on 09/16/2022 at 08:18:48 PM UTC

In C, how do you pick a random number between min and max?

Answer   Submitted by Answiki on 09/16/2022 at 08:18:10 PM UTC

In C, the rand() function is used to generate a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a macro defined in stdlib.h. To generate a number in a given range (between min and max), we can use one of the following solutions:

Integer :

// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);

Float :

// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));

When using these codes, remember to initialize the generator seed with srand() so as not to generate the same draws at each run. For more details, check this question :

9 events in history
Question by Answiki 09/16/2022 at 08:19:29 PM
In C, how to pick a random number in an interval?
Question by Answiki 09/16/2022 at 08:19:06 PM
In C, how to draw a random number in an interval?
Question by Answiki 09/16/2022 at 08:18:55 PM
In C, how to pick a random number between min and max?
Question by Answiki 09/16/2022 at 08:18:48 PM
In C, how do you pick a random number between min and max?
Question by Answiki 09/16/2022 at 08:18:16 PM
In C, how do you pick a random number in an interval?
Answer by Answiki on 09/16/2022 at 08:18:10 PM

In C, the rand() function is used to generate a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a macro defined in stdlib.h. To generate a number in a given range (between min and max), we can use one of the following solutions:

Integer :

// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);

Float :

// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));

When using these codes, remember to initialize the generator seed with srand() so as not to generate the same draws at each run. For more details, check this question :

Answer by Answiki on 09/16/2022 at 08:15:52 PM

In C, the rand() function is used to generate a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a macro defined in stdlib.h. To generate a number in a given range (between min and max), we can use one of the following solutions:

Integer :

// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);

Float :

// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));

When using it, remember to initialize the generator seed with srand() so as not to generate the same draws at each run. For more details, check this question :

Answer by Answiki on 09/16/2022 at 08:15:07 PM

In C, the rand() function is used to generate a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a macro defined in stdlib.h. To generate a number in a given range (between min and max), we can use one of the following solutions:

Integer :

// Pick a random number between min and max (included)
int x = min + rand() % (max + 1 - min);

Float :

// Pick a random floating point number between min and max (included)
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));

When using it, remember to initialize the generator seed with srand() so as not to generate the same draws at each run.

Question by Answiki 09/16/2022 at 08:11:11 PM
In C, how do you draw a random number in an interval?
# ID Query URL Count

Icons proudly provided by Friconix.