Question #514

How to pick a random number in C between 1 and 10?

Answer

To pick a random number between 1 and 10, the best option is to use the modulo to pick a random number between 0 and 9, and then to shift this number of 1:

// Pick a random number between 1 and 10
int value = rand()%10 + 1;

3 events in history
Question by Alphonsio 12/23/2020 at 04:32:46 PM
In C, how to pick a random number between 1 and 10?
Answer by Alphonsio 12/23/2020 at 04:32:26 PM

To pick a random number between 1 and 10, the best option is to use the modulo to pick a random number between 0 and 9, and then to shift this number of 1:

// Pick a random number between 1 and 10
int value = rand()%10 + 1;

Question by Alphonsio 12/23/2020 at 04:30:12 PM
How to pick a random number in C between 1 and 10?
# ID Query URL Count

Icons proudly provided by Friconix.