random number generator doubt in C

Status
Not open for further replies.

cedance

Advanced Member level 2
Joined
Oct 24, 2003
Messages
551
Helped
30
Reputation
60
Reaction score
7
Trophy points
1,298
Location
Germany
Activity points
4,622
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.

/cedance
 

srand() does not return a value. You are suppose to call srand() once to seed (initialize) the random number generator, and then call rand() multiple times to get random numbers based upon that seed.

In most C compilers, rand() is fast but not high quality. If you need high quality random numbers, search Google for web sites devoted to random number generators.
 

echo47,

i knew that i hav to use rand(). i was worried abt the seed value which is essential for the randomness. and i am also searching for a random generator using google. i thought i cud seek similar help with code or algorithm that wud favor me more in googling.

/cedance
 

You might try "lfsr random generator" in google.

I am using LFSR random genartors with good success but as you already wrote a "good" seed value is not so easy to find.

hope this helps
 

Getting a good seed depends on your operating system and hardware. What are you using?

The best method is probably a thermal noise generator connected to an A/D converter.

Some programs use the CPU's clock cycle counter, such as by using the Pentium's RDTSC opcode. It tells you the number of x-GHz clocks since the computer booted - a large fast-changing number.
 

hi guys,

u hav guys hav gone too far in hardware. i had my doubts inclined towards software, that too under C. i discovered the problem and solved it... and its that, the srand(...) shud be given only once in the progrma even if u hav many H files and corresponding C files... i jus removed all other srand() declarations and it worked fine. thks for ur suggestions thou.

/cedance
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…