[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

sintral

12/5/2008 10:20:00 PM

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).
12 Answers

john

12/5/2008 10:24:00 PM

0

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);

Pete Becker

12/5/2008 10:25:00 PM

0

On 2008-12-05 17:19:37 -0500, sintral <sintral@gmail.com> 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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jeff Schwab

12/6/2008 12:02:00 AM

0

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

#include <cassert>
#include <cstdlib>

template<typename T>
T get_random(T max =RAND_MAX) {
double const rand_max = RAND_MAX;
return std::rand() / rand_max * max;
}

void run() {
float const x = get_random<float>();
float const y = get_random<float>(x);
assert(0 <= x && x <= RAND_MAX);
assert(0 <= y && y <= x);
}

int main() {
run();
return EXIT_SUCCESS;
}

Andrey Tarasevich

12/6/2008 1:01:00 AM

0

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

sintral

12/6/2008 2:10:00 AM

0

On Dec 5, 8:01 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
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.

jason.cipriani@gmail.com

12/6/2008 2:21:00 AM

0

On Dec 5, 9:10 pm, sintral <sint...@gmail.com> wrote:
> On Dec 5, 8:01 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
> 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

Kai-Uwe Bux

12/6/2008 2:52:00 AM

0

sintral wrote:

> On Dec 5, 8:01 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
> 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

Ben Bacarisse

12/6/2008 3:49:00 AM

0

Pete Becker <pete@versatilecoding.com> writes:

> On 2008-12-05 17:19:37 -0500, sintral <sintral@gmail.com> 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.

Rolf Magnus

12/6/2008 9:56:00 AM

0

Pete Becker wrote:

> On 2008-12-05 17:19:37 -0500, sintral <sintral@gmail.com> 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.


Kai-Uwe Bux

12/6/2008 10:09:00 AM

0

Rolf Magnus wrote:

> Pete Becker wrote:
>
>> On 2008-12-05 17:19:37 -0500, sintral <sintral@gmail.com> 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