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

S

sintral

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

Noah Roberts

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

Does this work?

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

Andrey Tarasevich

sintral said:
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?
 
S

sintral

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?

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

jason.cipriani

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
 
K

Kai-Uwe Bux

sintral said:
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
 
B

Ben Bacarisse

Pete Becker said:
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!]
 
R

Rolf Magnus

Pete said:
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.
 
K

Kai-Uwe Bux

Rolf said:
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
 
B

Ben Bacarisse

Pete Becker said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top