Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Random float between 0 and 1, then a second random float between 0and 1 - x

Reply
Thread Tools

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

 
 
sintral
Guest
Posts: n/a
 
      12-05-2008
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).
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      12-05-2008
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);
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-06-2008
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

 
Reply With Quote
 
sintral
Guest
Posts: n/a
 
      12-06-2008
On Dec 5, 8:01*pm, Andrey Tarasevich <(E-Mail Removed)>
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.
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      12-06-2008
On Dec 5, 9:10*pm, sintral <(E-Mail Removed)> wrote:
> On Dec 5, 8:01*pm, Andrey Tarasevich <(E-Mail Removed)>
> 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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-06-2008
sintral wrote:

> On Dec 5, 8:01*pm, Andrey Tarasevich <(E-Mail Removed)>
> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-06-2008
Pete Becker <(E-Mail Removed)> writes:

> On 2008-12-05 17:19:37 -0500, sintral <(E-Mail Removed)> 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-06-2008
Pete Becker wrote:

> On 2008-12-05 17:19:37 -0500, sintral <(E-Mail Removed)> 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.


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-06-2008
Rolf Magnus wrote:

> Pete Becker wrote:
>
>> On 2008-12-05 17:19:37 -0500, sintral <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-07-2008
Pete Becker <(E-Mail Removed)> writes:

> On 2008-12-05 22:49:25 -0500, Ben Bacarisse <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments