C-programming-language

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

Share This Article with your friends if you appreciate my effort to write it!

  Start Learning With Wakish - Enroll Freely Now! (Click Here!)

Share Wakish Wonderz On Facebook
SUBSCRIBE ⇒ ∑ [ EMAILRSS ] wakish.info

Posted in: World Of Programming

  (3) Comments made - 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

Leave a Comment

Close
E-mail It