Oct 12, 2011

C Program to generate random numbers.

C Program to generate random numbers. Random Numbers are numbers which are produced by a process and its outcome is unpredictable.The function srand() is used to seed the random sequence. The rand() function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}] with a period of at least 2^32.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main() {
 int i, num;
 printf("Enter how many random numbers you want?\n");
 scanf("%d", &num);
 //The function srand() is used to seed the random sequence. 
 //srand() will generate a specific "random" sequence over and over again. 
 srand(time(NULL));
 //The rand() function shall compute a sequence of pseudo-random 
 //integers in the range [0, {RAND_MAX}]  with a period of at least 2^32.
 for (i = 0; i < num; i++) {
  printf("random_number[%d]= %d\n", i + 1, rand());
 }

 printf("A number between 0 and 99: %d\n", rand() % 100);

 printf("A number between 0 and 9: %d\n", rand() % 10);
 
 return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!