Question #6701   Submitted by Answiki on 09/21/2022 at 11:19:04 AM UTC

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

Answer   Submitted by 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;
}

8 events in history
Answer by 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;
}

Answer by 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;
}




Answer by 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;
}




Question by Answiki 09/21/2022 at 11:19:10 AM
How to pick a random number in C between 1 and 10 included?
Question by Answiki 09/21/2022 at 11:19:04 AM
How to pick a random number in C between 1 and 10 inclusive?
Question by Answiki 12/23/2020 at 04:32:46 PM
In C, how to pick a random number between 1 and 10?
Answer by 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;

Question by Answiki 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.