Random Numbers in C programming

Written by

C-programming-language random number

/* NOTE: This code has been coded with Borland C/C++ 3.0 */
//libraries
#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()

//main
int main()
{
//declaring a variable
int random;
//clearscreen
clrscr();

randomize();
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
getch();

}//end main

Note:

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 ;) )



Posted in: World Of Programming

  (10) Responses to "Random Numbers in C programming " - Say your part!

  1. 1
    From Gabriel BayNo Gravatar   on November 16th, 2009 at 6:18 pm

    how can we randomize number using probability?

  2. 2
    From Alexandre CostaNo Gravatar   on May 18th, 2010 at 6:43 pm

    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. 3
    From WakishNo Gravatar   on May 18th, 2010 at 6:50 pm

    Hi Alexandre,

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

    thanks

  4. 5
    From Mae delos ReyesNo Gravatar   on December 31st, 2010 at 12:26 pm

    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. 6
    From akash goyalNo Gravatar   on May 31st, 2011 at 11:43 pm

    thanks………..

  6. 7
    From priyankaNo Gravatar   on August 10th, 2011 at 1:55 pm

    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. 8
    From giselleNo Gravatar   on February 29th, 2012 at 1:06 am

    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. 9
    From WakishNo Gravatar   on March 7th, 2012 at 11:13 pm

    @giselle:
    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. 10
    From giselleNo Gravatar   on March 11th, 2012 at 4:51 am

    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:(

    #include
    #include

    int main()
    {
    int i, ri;

    for(i=1; i<=10; i++)
    {
    ri = rand() % 100+10; /* integer between 10-100 */
    printf("\%d\t%d\n",i,ri);
    }
    puts("generate 10 random numbers.");
    system("pause");
    return 0;
    }

This Page Has Been Mentioned By These Folks:

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

Use The Form Below To Leave Your Comment(s):

Feel free to use these tags in your comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>