Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*sigh* true random numbers... ?

Status
Not open for further replies.

zanza

Programmer
Feb 18, 2002
89
US
ok, i just searched for a while though the archives of tek tips for a decent random number generator... i found a few that i already know, but none quite fit what i want. how, without using 10,000 lines of code, would I be able to generate true random numbers (or at least closer to true random with seeding it with time)? žÅNžÅ
 

int getRandomValue()
{
static int init = 0;

// repeated calls to srand can produce same #
if(!init)
{
srand(time(NULL));
init++;
}

return rand();
}


Is that what you are looking for?

Matt
 
mayhap, ill try it out, but how would you go about limitimg how high the number can go?

int Random(int a)
{
int num;
srand ((time) (NULL));
num = rand() % a;
return num;
}

this is what im using, and a is passed to it as a limiting var žÅNžÅ
 
Hi,
As for where to put the Mod that is up to you. What you are doing with the parameter and the mod looks fine.

Your code has a problem with initilaizing SRAND().

Look at the first code. You will notice he only calls SRAND() the first time the function is called.

You don't want to keep seeding the random number generator on each loop.

---
 
Zyrenthian: look at man pages for static vars in 'c'
if you declare:
static int init = 0;
every time you enter the func init will be 0
so static is never static && the init++ does strictly nothing.
say: static int init; /* the compiler knows what's to do */
now init will be once 0, the first time you enter func,
i don't like: srand ((time) (NULL));
try the old fashion:
long now;
time(&now);
srand((int)now);


-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Been a long time since I have been

a. On unix
b. used C

But the theory is there.

i don't like: srand ((time) (NULL));
try the old fashion:
long now;
time(&now);
srand((int)now);


Matter of opinion... I like the other.

Heck make init global... course you probablly dont like global either. The point of the post was to demonstrate that you should seed the random number generator once. Concidering tdatgod understood it, I think I made my point.

Matt

 
"if you declare:
static int init = 0;
every time you enter the func init will be 0
so static is never static && the init++ does strictly nothing.
say: static int init; /* the compiler knows what's to do */
now init will be once 0, the first time you enter func,"


I don't know what compiler suffers from this flaw but every compiler I've ever used has no problem intializing a static variable on declaration. The initialization is only performed the first time the function is called.

As for getting a truly random number, if you don't want to use just the plain old [tt]srand((unsigned)time,NULL);[/tt] you could always use a combination of the current time AND something else that is constantly changing such as an algorythm generated from the current cursor position or soemthing like that.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Zyrenthian's code with the function static variable is perfectly fine. As gednick says, the initialization occurs only once, the first time the function is called.

As for "truly" random numbers, if you happen to be working on Linux (maybe other UNIX-like systems, but I'm not sure), there's a character device called /dev/random that returns numbers based on the entropy of your hardware.

If you don't mind working in C++, you can also check at for their random number generation library. Boost always has quality stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top