random number generator in C

Status
Not open for further replies.

legolas

Padawan
hi,

i have already asked for generation of random number generation in C language. But i hav a different problem now.

if we use the command srand(unsigned(time(NULL))) its true that i get random numbers generated based on time and so i hav possibility of getting random numbers bettr than if i used srand(some number) or dint use srand function at all.

but, in my program, i have to generate a random number and perform a function and then generate random number immediately after the function gets the value calculated... what happens is, i get the same random number being generated as the time it takes to calculate the function o/p is almost negligible and so the time (NULL) is practically the same value. I need to generate random numbers so rapid and calculate the same function. How can i do that? if not using the inbuilt function in C, cud u suggest me a link or code which does it. i also saw numerical receipes in C but it seems that the random numbers generated r not convincing in the way the o/p is given(as in case of binomial deviates or normal deviates) or its too diffuicult to understand and demands input and user functions to be written(as in the case of adaptive mone-carlo method).

pls suggest me a simple yet good random number generator or help me using the inbuilt function in C itself. thank you.

/legolas
 

sakumar79

Technomancer
Are u using srand twice, both after and before the function? If so, there is your problem. Call srand function only once. Then use rand() function to get multiple random numbers.

srand(unsigned(time(NULL)));
// Output random values.
cout<< rand() << endl;
cout<< rand() << endl;
cout<< rand() << endl;

Arun
 

siriusb

Cyborg Agent
Yes, that is your problem because latency is not the problem as there is time involved in shifting function address and arguments in the stack. The srand is only to initialize the rand number generator's seed to a different value each time. Or else, every time you quit the program and call rand(), it will return the same sequence of numbers as rand number. This is because the seed is the same everytime.
This is why these functions are called pseudo-random number generators. Real random number generators are costly harware modules that generate numbers based on some realtime noise like line noise, radiation, etc.
 
OP
L

legolas

Padawan
hi,

thks for the real quick reply. i hav done that mistake and wil change it and reply back. but b4 that, i hav another question.

i hav a main program which uses random number and so i hav used srand(....) there and also i hav functions which are written in another C file, defined in a user-defined H file.. for my functions also, they need random numbers. so shud i give srand(..) there too or is it enough if i give in the main C file and jus include the header files and cll the functions? hope i am clear enuf.

/legolas
 
Status
Not open for further replies.
Top Bottom