# drand48() Probability

• 02-14-2008
BENCHMARKMAN
drand48() Probability
I am trying to use drand48() with a .6 probability of getting a 1. How can I do this?
• 02-15-2008
matsp
So, you want a distorted distribution, where you have 60% chance of 1.0, and 40% chance of 0 to "just under 1"? How do you want the second part of the distribution to be formed?

If you want the 40% portion to be linear, perhaps this would work:
Code:

```double myrand() {   double x;       x = drand48();   if (x >= 0.6) x = 1;   else x = x / 0.6;  /* Extend the range to 0..0.999999 */ }```
--
Mats
• 02-15-2008
iMalc
Quote:

Originally Posted by BENCHMARKMAN
I am trying to use drand48() with a .6 probability of getting a 1. How can I do this?

That's a bit like asking for a 5-letter word containing an 'E'.
What kind of distribution are you after, and what are the minimum and maximum values of the resulting random numbers?

Are you just wanting a one or a zero? Something like this perhaps?:
return drand48() >= 0.4 ? 1 : 0;
• 02-15-2008
fractal
i am sorry if this is off topic, but i am curious.
Is this "drand48()" function ANSI C89?
• 02-15-2008
robwhit
I don't think so.
• 02-16-2008
BENCHMARKMAN
I am looking to have 60% chance of getting a 1 and 40% chance of getting a 0.
• 02-16-2008
laserlight
Quote:

Is this "drand48()" function ANSI C89?
It is non-standard with respect to C.

Quote:

I am looking to have 60&#37; chance of getting a 1 and 40% chance of getting a 0.
I think you can use iMalc's suggestion.
• 02-18-2008
BENCHMARKMAN
Thanks for the replies. I am also supposed to use srand48() to nitialize The random number stream with a seed of 9. I'm a little confused here. Somebody explain this?
• 02-18-2008
xuftugulus
I am not sure, but ... srand48(9) might be the answer.
• 02-18-2008
matsp
So call
Code:

` srand48(9L);`
That will guarantee that the random numbers are always the same every time.

--
Mats
• 02-18-2008
BENCHMARKMAN
but how do I use srand()48 with drand48(). I'm confused.
• 02-18-2008
tabstop
Quote:

Originally Posted by BENCHMARKMAN
but how do I use srand()48 with drand48(). I'm confused.

Just like srand() with rand() in standard C, srand48() just sets the "seed" value for the random number generator. Fixing the seed value means that the random number generator gives the same values every time you run the program. So, as mentioned above in multiple places, pass the seed as a parameter with srand48() once at the very beginning of the program.