In C, how to pick a random number between 1 and 10?
In C, 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:
The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:// Pick a random number between 1 and 10
int value = rand()%10 + 1;
#include <stdio.h> // For printf
#include <stdlib.h> // For rand() srand()
#include <time.h> // For time()
int main(void) {
// Initialize the pseudo random generator
// Without this line, we would always pick the same number
srand(time(NULL));
// Generate a random number between 1 and 10 (included)
int x = rand()%10 + 1;
// Display the number
printf("Random number : %d\n", x );
return 0;
}
In C, 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:
The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:// Pick a random number between 1 and 10
int value = rand()%10 + 1;
#include <stdio.h> // For printf
#include <stdlib.h> // For rand() srand()
#include <time.h> // For time()
int main(void) {
// Initialize the pseudo random generator
// Without this line, we would always pick the same number
srand(time(NULL));
// Generate a random number between 1 and 10 (included)
int x = rand()%10 + 1;
// Display the number
printf("Random number : %d\n", x );
return 0;
}
In C, 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:
The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:// Pick a random number between 1 and 10
int value = rand()%10 + 1;
#include <stdio.h> // For printf
#include <stdlib.h> // For rand() srand()
#include <time.h> // For time()
int main(void) {
// Initialize the pseudo random generator
// Without this line, we would always pick the same number
srand(time(NULL));
// Generate a random number between 1 and 10 (included)
int x = rand()%10 + 1;
// Display the number
printf("Random number : %d\n", x );
return 0;
}
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:
The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:// Pick a random number between 1 and 10
int value = rand()%10 + 1;
#include <stdio.h> // For printf
#include <stdlib.h> // For rand() srand()
#include <time.h> // For time()
int main(void) {
// Initialize the pseudo random generator
// Without this line, we would always pick the same number
srand(time(NULL));
// Generate a random number between 1 and 10 (included)
int x = rand()%10 + 1;
// Display the number
printf("Random number : %d\n", x );
return 0;
}
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;
# | ID | Query | URL | Count |
---|---|---|---|---|
0 | 7091 | https://en.ans.wiki/305/in-c-how-to-pick-a-random-number-between-1-and-10/ | https://en.ans.wiki/305/in-c-how-to-pick-a-random-number-between-1-and-10 | 1 |