Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-11-2005, 10:53 PM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default random number generator in C


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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-11-2005, 11:12 PM   #2 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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
sakumar79 is offline  
Old 02-11-2005, 11:23 PM   #3 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default

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.
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
siriusb is offline  
Old 02-11-2005, 11:30 PM   #4 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by clmlbx
- by gforz
- by Who

Advertisement




All times are GMT +5.5. The time now is 04:43 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2