**Answiki**on 12/23/2020 at 04:32:46 PM UTC

# In C, how to pick a random number between 1 and 10?

**Answiki**on 09/21/2022 at 11:25:10 AM UTC

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:

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

The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:```
#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;
}
```

**Answiki**on 09/21/2022 at 11:25:10 AM

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:

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

The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:```
#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;
}
```

**Answiki**on 09/21/2022 at 11:25:05 AM

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:

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

The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:```
#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;
}
```

**Answiki**on 09/21/2022 at 11:20:36 AM

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;
```

The generic formula is detailed here. Bellow is the complete code that initializes the pseudorandom generator to avoid always drawing the same numbers:```
#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;
}
```

**Answiki**09/21/2022 at 11:19:10 AM

**Answiki**09/21/2022 at 11:19:04 AM

**Answiki**12/23/2020 at 04:32:46 PM

**Answiki**on 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;
```

**Answiki**12/23/2020 at 04:30:12 PM

# | 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 |