Random Numbers in C programming

Want To Receive Secret Quality Tips Weekly?
Subscribe FREELY to my Email List - click here!

C-programming-language random number

/* NOTE: This code has been coded with Borland C/C++ 3.0 */
#include <stdio.h> //defines scanf() and printf()
#include <stdlib.h> //defines rand()
#include <time.h> //defines time() and is needed for randomize()
#include <conio.h> //defines clrscr() and getch()

int main()
//declaring a variable
int random;

random = random(100);

//display result
printf(“\nRandom numbers in the range 0-99: %d”,random);

//wait for a key to be pressed by user before program exits

}//end main


1) The randomize() initialises the random number generator, rand(), with a value.

2) The code can work without the randomize() function, BUT without it your computer will only generate same number each time.
So, the randomize() is there to generate different random numbers with “time” and hence u need to include the library file <time.h>

3) The function rand(100) generates random numbers between 0 and (100-1). That is numbers between 0-99.

4) If you want to display 20 random numbers, just call the generator in a ‘for loop‘ with “counter <= 20“, like for(int counter=0;counter <= 20; counter++)

That’s all folks, simple huh? :) (Yeah, but effective 😉 )

Valuable Feedback / Comment / Review From People Like You

  1. Gabriel Bay says:

    how can we randomize number using probability?

  2. Alexandre Costa says:

    1) The randomize() initialises the random number generator, rand(), with a value.

    Hey, yeah, i’ve read that in manny places so far, the thing is, i can get the random numbers, but has u said always the same numbers.. when i try to put “randomize();” in my program, when compiling it it show
    implicit declaration of funcion ‘Randomize’…
    know what am i doing wrong?.. cheers

  3. Wakish says:

    Hi Alexandre,

    can you please tell me the following:
    1) what compiler are you using?
    2) can you paste the whole code here please?


  4. Mae delos Reyes says:

    Hello, thanks for that article, I would just like to ask if what are you going to add if you don’t want your program to generate the same number again. We were assigned to do the lottery system wherein we need to generate 6 random numbers from 1-55, with no repetition. Thanks!

  5. akash goyal says:


  6. priyanka says:

    please help me out to write the C code for generating a random float number between 0 and 1….. i will be very thankfull to you..

  7. giselle says:

    Hi, i am new c++ user and i have a problem. Is there anyone to help me?

    Question is; “Write random numbers between 10 and 100increasing by 10 with c++”

    Please help me!

  8. Wakish says:

    use same code with MIN = 10, MAX=100
    Btw, increasing by 10, are you sure about this? Since it will not make it appear “random”, it will more likely to produce a pattern and hence fail in being random..

  9. giselle says:

    Thank you for the message. it is necessary to have my output numbers like “10,30,40,20,60,50,…”
    i’ve tried to write program like below; but this generate any random numbers between 10-100 but multiplication by 10. i missed something i think but still couldn’t find it:(


    int main()
    int i, ri;

    for(i=1; i<=10; i++)
    ri = rand() % 100+10; /* integer between 10-100 */
    puts("generate 10 random numbers.");
    return 0;

These Folks Mentioned This Article Somewhere..

  1. [POST] C + Gnuplot to analyze digital signal process « Tech Notes says:

    […]     how to generate random numbers: http://wakish.info/random-numbers-in-c-programming/ […]

Speak Your Mind