# Random float between 0 and 1, then a second random float between 0and 1 - x

Discussion in 'C++' started by sintral, Dec 5, 2008.

1. ### sintralGuest

I'm sure this is simple, I've tried to find it in the group, but no
luck.

I'm generating one random float between 0 and 1: x = (float) rand()/
RAND_MAX;
How to I generate a second random number between 0 and my 1st random
number; 1 - x?

I saw between because the sum of x and y must be < 1; (non-inclusive).

sintral, Dec 5, 2008

2. ### Noah RobertsGuest

Re: Random float between 0 and 1, then a second random float between0 and 1 - x

sintral wrote:
> I'm sure this is simple, I've tried to find it in the group, but no
> luck.
>
> I'm generating one random float between 0 and 1: x = (float) rand()/
> RAND_MAX;
> How to I generate a second random number between 0 and my 1st random
> number; 1 - x?
>
> I saw between because the sum of x and y must be < 1; (non-inclusive).

Does this work?

y = (float)rand()/RAND_MAX * (1-x);

Noah Roberts, Dec 5, 2008

3. ### Andrey TarasevichGuest

Re: Random float between 0 and 1, then a second random float between0 and 1 - x

sintral wrote:
>
> I'm generating one random float between 0 and 1: x = (float) rand()/
> RAND_MAX;
> How to I generate a second random number between 0 and my 1st random
> number; 1 - x?

Sorry, but this last part is ambiguous.

I assume that 'x' stand for your first random number. Right? You are
saying that you need another number "between 0 and my 1st random
number", which means between 0 and x. But in the subject you are saying
that you need one "between 0 and 1-x". So what is it you need as the
second number? What is the upper limit of the second range: x or 1-x?

--
Best regards,
Andrey Tarasevich

Andrey Tarasevich, Dec 6, 2008
4. ### sintralGuest

Re: Random float between 0 and 1, then a second random float between0 and 1 - x

On Dec 5, 8:01 pm, Andrey Tarasevich <>
wrote:
> sintral wrote:
>
> > I'm generating one random float between 0 and 1: x = (float) rand()/
> > RAND_MAX;
> > How to I generate a second random number between 0 and my 1st random
> > number; 1 - x?

>
> Sorry, but this last part is ambiguous.
>
> I assume that 'x' stand for your first random number. Right? You are
> saying that you need another number "between 0 and my 1st random
> number", which means between 0 and x. But in the subject you are saying
> that you need one "between 0 and 1-x". So what is it you need as the
> second number? What is the upper limit of the second range: x or 1-x?
>
> --
> Best regards,
> Andrey Tarasevich

Yes, you're right I worded that wrong. The second random number should
be between x and 1 - x.

sintral, Dec 6, 2008
5. ### Guest

Re: Random float between 0 and 1, then a second random float between0 and 1 - x

On Dec 5, 9:10 pm, sintral <> wrote:
> On Dec 5, 8:01 pm, Andrey Tarasevich <>
> wrote:
>
>
>
> > sintral wrote:

>
> > > I'm generating one random float between 0 and 1: x = (float) rand()/
> > > RAND_MAX;
> > > How to I generate a second random number between 0 and my 1st random
> > > number; 1 - x?

>
> > Sorry, but this last part is ambiguous.

>
> > I assume that 'x' stand for your first random number. Right? You are
> > saying that you need another number "between 0 and my 1st random
> > number", which means between 0 and x. But in the subject you are saying
> > that you need one "between 0 and 1-x". So what is it you need as the
> > second number? What is the upper limit of the second range: x or 1-x?

>
> > --
> > Best regards,
> > Andrey Tarasevich

>
> Yes, you're right I worded that wrong. The second random number should
> be between x and 1 - x.

Boundary conditions aside it's just the normal scale one range of
values to another... in this case you scale [0,RAND_MAX] to [x,1-x].

outvalue = (invalue - inmin) / (inmax - inmin) * (outmax - outmin) +
outmin;

Note that it doesn't have to be the case that inmax > inmin or outmax
> outmin, just as long as (inmax - inmin) isn't 0.

Doing the algebra and plugging the appropriate values into the above
equation is left as an exercise.

Jason

, Dec 6, 2008
6. ### Kai-Uwe BuxGuest

Re: Random float between 0 and 1, then a second random float between 0 and 1 - x

sintral wrote:

> On Dec 5, 8:01 pm, Andrey Tarasevich <>
> wrote:
>> sintral wrote:
>>
>> > I'm generating one random float between 0 and 1: x = (float) rand()/
>> > RAND_MAX;
>> > How to I generate a second random number between 0 and my 1st random
>> > number; 1 - x?

>>
>> Sorry, but this last part is ambiguous.
>>
>> I assume that 'x' stand for your first random number. Right? You are
>> saying that you need another number "between 0 and my 1st random
>> number", which means between 0 and x. But in the subject you are saying
>> that you need one "between 0 and 1-x". So what is it you need as the
>> second number? What is the upper limit of the second range: x or 1-x?
>>
>> --
>> Best regards,
>> Andrey Tarasevich

>
> Yes, you're right I worded that wrong. The second random number should
> be between x and 1 - x.

Still not clear: is your number x between 0 and 1/2 or between 0 and 1. In
the latter case, what is the range [x,1-x] for x > 1/2? There are two
obvious interpretations:

a) [0.7, 0.3] = [0.3, 0.7]
b) [0.7, 0.3] is empty.

Which one do you need?

Best

Kai-Uwe Bux

Kai-Uwe Bux, Dec 6, 2008
7. ### Ben BacarisseGuest

Re: Random float between 0 and 1, then a second random float between 0 and 1 - x

Pete Becker <> writes:

> On 2008-12-05 17:19:37 -0500, sintral <> said:
>
>> I'm sure this is simple, I've tried to find it in the group, but no
>> luck.
>>
>> I'm generating one random float between 0 and 1: x = (float) rand()/
>> RAND_MAX;
>> How to I generate a second random number between 0 and my 1st random
>> number; 1 - x?
>>
>> I saw between because the sum of x and y must be < 1; (non-inclusive).

>
> Generate a number between 0 and 1, and multiply it by (1-x).
>
> But there are some boundary conditions that you have to watch for. In
> particular, (float)rand()/RAND_MAX can be 1, and in that case you
> can't generate a non-negative value that you can add to x to give you
> a value that's < 1. You really need to divide by (RAND_MAX + 1), to
> generate a value that's in the range 0 <= x < 1. But you have to be
> careful there, because if RAND_MAX is equal to UINT_MAX, adding 1 to
> it will give you 0.

I don't think this quite right. rand() returns int and in the (rare)
case where INT_MAX == UINT_MAX, adding 1 is implementation defined. 0
is presumably possible, but it seems unlikely in practise.

Converting to a wider integer type (if there is one) or to unsigned
(if UINT_MAX > INT_MAX) may help, but even so, float often does not
have enough precision:

(float)rand() / ((unsigned long)RAND_MAX + 1)

can be exactly == 1.0. I would avoid float for this purpose
altogether.

[Aside: this can happen even using (double)rand() when the int
returned by rand() is 64 bits. There is a lot of code that relies on
this division being strictly less that 1 (to generate array indexes
for example) that will break with a 64 rand() function!]

--
Ben.

Ben Bacarisse, Dec 6, 2008
8. ### Rolf MagnusGuest

Re: Random float between 0 and 1, then a second random float between 0 and 1 - x

Pete Becker wrote:

> On 2008-12-05 17:19:37 -0500, sintral <> said:
>
>> I'm sure this is simple, I've tried to find it in the group, but no
>> luck.
>>
>> I'm generating one random float between 0 and 1: x = (float) rand()/
>> RAND_MAX;
>> How to I generate a second random number between 0 and my 1st random
>> number; 1 - x?
>>
>> I saw between because the sum of x and y must be < 1; (non-inclusive).

>
> Generate a number between 0 and 1, and multiply it by (1-x).
>
> But there are some boundary conditions that you have to watch for. In
> particular, (float)rand()/RAND_MAX can be 1, and in that case you can't
> generate a non-negative value that you can add to x to give you a value
> that's < 1. You really need to divide by (RAND_MAX + 1), to generate a
> value that's in the range 0 <= x < 1. But you have to be careful there,
> because if RAND_MAX is equal to UINT_MAX, adding 1 to it will give you
> 0. None of this is really hard, but you have to be a little careful.
> Details are left as an exercise.

Another thing is that on most platforms, float cannot store the exacxt value
of RAND_MAX. double would be a better choice.

Rolf Magnus, Dec 6, 2008
9. ### Kai-Uwe BuxGuest

Re: Random float between 0 and 1, then a second random float between 0 and 1 - x

Rolf Magnus wrote:

> Pete Becker wrote:
>
>> On 2008-12-05 17:19:37 -0500, sintral <> said:
>>
>>> I'm sure this is simple, I've tried to find it in the group, but no
>>> luck.
>>>
>>> I'm generating one random float between 0 and 1: x = (float) rand()/
>>> RAND_MAX;
>>> How to I generate a second random number between 0 and my 1st random
>>> number; 1 - x?
>>>
>>> I saw between because the sum of x and y must be < 1; (non-inclusive).

>>
>> Generate a number between 0 and 1, and multiply it by (1-x).
>>
>> But there are some boundary conditions that you have to watch for. In
>> particular, (float)rand()/RAND_MAX can be 1, and in that case you can't
>> generate a non-negative value that you can add to x to give you a value
>> that's < 1. You really need to divide by (RAND_MAX + 1), to generate a
>> value that's in the range 0 <= x < 1. But you have to be careful there,
>> because if RAND_MAX is equal to UINT_MAX, adding 1 to it will give you
>> 0. None of this is really hard, but you have to be a little careful.
>> Details are left as an exercise.

>
> Another thing is that on most platforms, float cannot store the exacxt
> value of RAND_MAX. double would be a better choice.

True, but the story is a little more complicated. You would want RAND_MAX+1
anyway; that usually is a power of 2 and can be represented exactly by a
float. Now, it is implementation defined whether converting RAND_MAX to a
float will yield the power of 2 or the largest representable number below.
However, even if converting to float will do the RightThing(tm), you run
into trouble since some random values will also be converted to the same
float. In that case, the quotient is 1 and not <1.

Best

Kai-Uwe Bux

Kai-Uwe Bux, Dec 6, 2008
10. ### Ben BacarisseGuest

Re: Random float between 0 and 1, then a second random float between 0 and 1 - x

Pete Becker <> writes:

> On 2008-12-05 22:49:25 -0500, Ben Bacarisse <> said:
>
>>
>> I don't think this quite right.

>
> As I said (and as you snipped), the details are left as an exercise.

You make it sound as if I spoilt the fun! I didn't explain the
details of how to portably add 1 to RAND_MAX. In addition, I pointed
out another problem but, again, I did not explain how to get round it
in general.

The problem of how to generate a float in [0,1) from a call to rand()
is still there as an exercise for the OP.

--
Ben.

Ben Bacarisse, Dec 7, 2008