# 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.

## 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 on other sites
There is no standard random() function in either C or C++. You have rand() and srand().

##### Share on other sites
I see, then there is some code missing it seems.

Thanks

##### 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 on other sites
Maybe rand() and random() return different things.

##### 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 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 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 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 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

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011388
• ### Who's Online (See full list)

There are no registered users currently online

×