Jump to content
  • Advertisement
Sign in to follow this  
Mr Lane

rand() and random()

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some code here that isnt mine, and it uses a function random(). When I compile the sole error I get is with this function, basically its undefined. I assume then thats is some standard C/C++ built in, but then why is the linker unable to find it? When I use the rand() func in its place the program compiles...but freezes when it runs... I *vaguely* remember using a function called random() in a previous project, but I cant remember if it was a built in or if it was given to us in the code. I vaguely remember it being given to us, but it used rand() itself...but I cant be sure. This time its definitely not given, so is random() a std:: or stdlib built in?

Share this post


Link to post
Share on other sites
Advertisement
There is no standard random() function in either C or C++. You have rand() and srand().

Share this post


Link to post
Share on other sites
Reading this Linux man page random() certainly is a stdlib function...looking through my stdlib.h its not there.

What I dont get is why using rand() in this program freezes it...

Share this post


Link to post
Share on other sites
Yeah I am taking a look at this. random() by the way is standard in Unix implementations I have found out.

Share this post


Link to post
Share on other sites
Not having seen the code, it could be that, of course, rand() is returning an unsigned integer, whereas the unknown function random() is an external function that converts rand() to a double, something like:



double random()
{
return rand()/(1.0 + RAND_MAX); // you might need a static_cast<>(double) here.
}




There are some very efficient gaussian and exponential generators out there that use both unsigned int and double in the same algorithm.

--random

Share this post


Link to post
Share on other sites
This random() function can be either the Linux-stdlib function or a user-implemented function that is missing. But in the most likely first case, it should behave like rand() according to that same man page.

Greetz,

Illco

Share this post


Link to post
Share on other sites
rand() produces values clamped between 0 and MAX_RAND in stlib.h. random() on the other hand is between 0 and (2^31)-1.

I am emulating random() with rand() by doing the following:


int GetRand(void)
{
int n;
while(1)
{
n = rand()*rand()*rand();
if((n > 2147483647) || (n < 0))
;
else
break;
}
return n;
}


Apparently random() is also "more random" than rand() as well...hmmm

Share this post


Link to post
Share on other sites
The reason for values between 0 and (2^31)-1 is to avoid undefined behaviour when using the base random values in lognormal or some forms of exponential or gamma generators.

The function random() that I gave above is for the interval [0,1). Are you sure that your data types are correct within the context of your code?

--random

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!