Question #513

How to pick a random number in C?

Answer

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant defined in stdlib.h, its value may vary according to the compiler. Here is a complete example:

// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {

  // Set the generator seed (set it once)
  // This prevent from always generating the same numbers
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generator from always returning the same numbers, we generally initialise the seed with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));

6 events in history
Answer by Alphonsio 12/23/2020 at 04:25:43 PM

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant defined in stdlib.h, its value may vary according to the compiler. Here is a complete example:

// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {

  // Set the generator seed (set it once)
  // This prevent from always generating the same numbers
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generator from always returning the same numbers, we generally initialise the seed with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));

Answer by Alphonsio 12/23/2020 at 04:24:58 PM

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant defined in stdlib.h, its value may vary according to the compiler. Here is a complete example:

// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {

  // Set the generator seed (set it once)
  // This prevent from always generating the same numbers
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generating from always providing the same sequence of numbers, we generally initialise the seed with the current time. The following line initialize the pseudo-random generator with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));

Answer by Alphonsio 12/23/2020 at 04:24:53 PM

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant defined in stdlib.h, its value may vary according to the compiler. Here is a complete example:

// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {

  // Set the generator seed (set it once)
  // This prevent from always generating the same numbers
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generating from always providing the same sequence of numbers, we generally initialise the seed with the current time. The following line initialize the pseudo-random generator with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));



Answer by Alphonsio 12/23/2020 at 04:24:02 PM

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant defined in stdlib.h, its value may vary according to the compiler. Here is a complete example:

// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {

  // Set the generator seed (set it once)
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generating from always providing the same sequence of numbers, we generally initialise the seed with the current time. The following line initialize the pseudo-random generator with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));



Answer by Alphonsio 12/23/2020 at 04:22:45 PM

In C, the rand() function returns a pseudo-random number between 0 and RAND_MAX. Here is a complete example :


// For printf()
#include <stdio.h>
// For srand() and rand()
#include <stdlib.h>
// For time()
#include <time.h>

int main(void) {
  // Set the generator seed (set it once)
  srand(time(NULL));

  // Pick two pseudo-random numbers
  printf ("First random number: %d\n", rand());
  printf ("Second random number: %d\n", rand());
  return 0;
}


Note: to prevent the generating from always providing the same sequence of numbers, we generally initialise the seed with the current time. The following line initialize the pseudo-random generator with the current time that is always changing, modifying the sequence of random numbers. If the seed is not set, the numbers will always be the same.

srand(time(NULL));



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

Icons proudly provided by Friconix.