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. 2
    From Alexandre Costa   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

    2
    From Alexandre Costa   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

  2. 6
    From akash goyal   on May 31st, 2011 at 11:43 pm

    thanks………..

    6
    From akash goyal   on May 31st, 2011 at 11:43 pm

    thanks………..

  3. 8
    From giselle   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
    From giselle   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!

  4. 10
    From giselle   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;
    }

    10
    From giselle   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>