Perfect Random Number Generator in C/C++
Posted: Thu Mar 18, 2010 10:58 pm
Note that the function GetRandomNumber accepts two parameters. These are the lower and upper limits (inclusive) of random numbers that are generated.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // for time()
// Generate a random number between nLow and nHigh (inclusive)
unsigned int GetRandomNumber(int nLow, int nHigh){
return (rand() % (nHigh - nLow + 1)) + nLow;
}
void main(){
int i;
srand(time(0)); // set initial seed value to system clock
// Generate 100 random numbers
for (i = 0; i < 100; i++){
printf("%d\t", GetRandomNumber(0, 3));
}
}