Random Numbers in C programming
/* 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
)

how can we randomize number using probability?
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
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
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!
thanks………..
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..
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!
@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..
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;
}