GoHacking.com: Ethical Hacking and Cyber Security
Home » C PROGRAMMING

C Program to Generate Random Numbers

Submitted by on Wednesday, 16 January 20088 Comments

This is a simple program to generate random numbers. This logic can be used to build a Lotto program or a program to pick Lucky number and so on. Here’s the program

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

int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));
printf(“Ten random numbers from 0 to 99\n\n”);
for(i=0; i<10;i++)
printf(“%d\n”,rand()%100);
}

OR
If it necessary to generate a number between 0 and (num-1) then this program meets the ideal solution
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/* prints a random number in the range 0 to 99 */
int main(void)
{
randomize();
printf(“Random number in the 0-99 range: %d\n”, random (100));
return 0;
}



By using/following this site you agree to our Legal Disclaimer

8 Comments »

  • bumfluf said:

    Thats great! I’ve been looking all over for this solution, thanks!

  • Anonymous said:

    More of a question to anyone, Is their a program to figure out what random number algorithm (prng)a computer is useing to generate random numbers.

  • aditya said:

    hai sir,

    could u please explain the working of function srand();

  • Srikanth (author) said:
  • aswin said:

    hi sirkanth ,
    well i am newbie in C programming , just 15 years old and i am very curious about learning C programming . I am attending a course form a private institution and there they are very slow with the portions . SO i am thiking studying of my own , but will i get the logics required if i am learning my own to prgram looking at books

  • Srikanth (author) said:

    @ aswin

    Studying on your own is the best way to learn.. In fact I learnt programming only through books and not from any other source..

  • Siddhant said:

    I am totally new to C…can you help me with the code structure written above and what should I refer to learn C…
    Please help me with it
    And one more thing, from where shall I download the program C?
    Awaiting Response
    My email ID is “[email protected]
    Thanks

  • someone said:

    this is another way to do it

    #include
    using namespace std;
    #include
    #include

    int main ()
    {

    int number;
    //seeds the random number
    srand(time(0));

    //generates a # from 1 to 100
    number = (rand()%100)+1;

    //prints to screen
    cout << random;

    }

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.