From: h2s
Subject: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <d7759320-6165-461a-bc9d-66ad9090780f@l33g2000pri.googlegroups.com>
i would like to generate random numbers in the range -2.0 to
+3.0.please help.thanks in advance

From: stassats
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <252277ad-5c75-47af-b5bf-d44629a858a0@y21g2000hsf.googlegroups.com>
On Aug 3, 7:21 am, h2s <··········@gmail.com> wrote:
> i would like to generate random numbers in the range -2.0 to
> +3.0.please help.thanks in advance

(- (random 5.0) 2.0)
From: Don Geddis
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <87abfsmj0l.fsf@geddis.org>
stassats <········@gmail.com> wrote on Sat, 2 Aug 2008 :
> On Aug 3, 7:21�am, h2s <··········@gmail.com> wrote:
>> i would like to generate random numbers in the range -2.0 to
>> +3.0.please help.thanks in advance
> (- (random 5.0) 2.0)

How often will that code return the result +3.0?

Or, perhaps the original spec didn't provide enough detail...
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
Many people would have given up on the town.  But we decided to stay.  Stay,
and make fun of the things they were trying to do to make the town better,
because we couldn't afford to leave and we really didn't want to help out.
	-- Deep Thoughts, by Jack Handey [1999]
From: Pascal J. Bourguignon
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <87od48roaw.fsf@hubble.informatimago.com>
Don Geddis <···@geddis.org> writes:

> stassats <········@gmail.com> wrote on Sat, 2 Aug 2008 :
>> On Aug 3, 7:21�am, h2s <··········@gmail.com> wrote:
>>> i would like to generate random numbers in the range -2.0 to
>>> +3.0.please help.thanks in advance
>> (- (random 5.0) 2.0)
>
> How often will that code return the result +3.0?

Never.


> Or, perhaps the original spec didn't provide enough detail...

It says 'less':  (< (random 5.0) 5.0)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"A TRUE Klingon warrior does not comment his code!"
From: John Thingstad
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <op.ufesxwxcut4oq5@pandora.alfanett.no>
P� Tue, 05 Aug 2008 01:56:42 +0200, skrev Don Geddis <···@geddis.org>:

> stassats <········@gmail.com> wrote on Sat, 2 Aug 2008 :
>> On Aug 3, 7:21�am, h2s <··········@gmail.com> wrote:
>>> i would like to generate random numbers in the range -2.0 to
>>> +3.0.please help.thanks in advance
>> (- (random 5.0) 2.0)
>
> How often will that code return the result +3.0?
>

Think of it -2.0 to 3.0  has a infinite number of values. Look up Hilbert  
hotel in Wikipedia.
So you would expect "never". Of course no random generator is perfect.
Like Pascal said a scaling of the range 1.0 to 5.0 followed by a transform  
to -2.0 would be the obvious approach.
If you "need" more granularity take a fixed integer rage say 0..100 onto  
this range. This WOULD kiss the limits.

--------------
John Thingstad
From: Kent M Pitman
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <u1w138jik.fsf@nhplace.com>
"John Thingstad" <·······@online.no> writes:

> På Tue, 05 Aug 2008 01:56:42 +0200, skrev Don Geddis <···@geddis.org>:
> 
> > stassats <········@gmail.com> wrote on Sat, 2 Aug 2008 :
> >> On Aug 3, 7:21 am, h2s <··········@gmail.com> wrote:
> >>> i would like to generate random numbers in the range -2.0 to
> >>> +3.0.please help.thanks in advance
> >> (- (random 5.0) 2.0)
> >
> > How often will that code return the result +3.0?
> >
> 
> Think of it -2.0 to 3.0  has a infinite number of values. Look up
> Hilbert  hotel in Wikipedia.
> So you would expect "never". Of course no random generator is perfect.
> Like Pascal said a scaling of the range 1.0 to 5.0 followed by a
> transform  to -2.0 would be the obvious approach.
> If you "need" more granularity take a fixed integer rage say 0..100
> onto  this range. This WOULD kiss the limits.

Yeah, I was going to respond the same except of course that the float are
not infinitely dense, and so the probability is decidedly more non-zero
than the real number probability.

I think Don was basically just saying that given this fact, it's
useful at minimum to know if one end or the other of the OP's desired
range is intended to be open.  

Then again, if the range is intended to be closed on both ends, the
floats being what they are, there is a specific least float bigger
than 3 such that it is a perfectly useful open upper bound to solve
the problem. :) And yes, I'm too lazy to go compute what it would be.
(Although... hmmm... because I think the floating point epsilon
actually varies with magnitude of the float, there might be some
slight messiness of adjusting the range by addition/subtraction.  I
didn't think hard on this matter and whether it leaves a gap in any
odd place.)
From: Raymond Toy
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <sxdbq0788j4.fsf@rtp.ericsson.se>
>>>>> "Kent" == Kent M Pitman <Kent> writes:

    Kent> Then again, if the range is intended to be closed on both ends, the
    Kent> floats being what they are, there is a specific least float bigger
    Kent> than 3 such that it is a perfectly useful open upper bound to solve
    Kent> the problem. :) And yes, I'm too lazy to go compute what it would be.
    Kent> (Although... hmmm... because I think the floating point epsilon
    Kent> actually varies with magnitude of the float, there might be some
    Kent> slight messiness of adjusting the range by addition/subtraction.  I
    Kent> didn't think hard on this matter and whether it leaves a gap in any
    Kent> odd place.)

I think this will work, but I'm not 100% sure:

(defun next-after (d)
  (multiple-value-bind (frac exp sign)
      (integer-decode-float d)
    (* sign (scale-float (float (1+ frac) d) exp))))

(next-after 3.0) -> 3.0000002

Ray
From: Tamas K Papp
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <6frhvrFd089oU2@mid.individual.net>
On Tue, 05 Aug 2008 13:21:55 -0400, Kent M Pitman wrote:

> Then again, if the range is intended to be closed on both ends, the
> floats being what they are, there is a specific least float bigger than
> 3 such that it is a perfectly useful open upper bound to solve the
> problem. :) And yes, I'm too lazy to go compute what it would be.
> (Although... hmmm... because I think the floating point epsilon actually
> varies with magnitude of the float, there might be some slight messiness
> of adjusting the range by addition/subtraction.  I didn't think hard on
> this matter and whether it leaves a gap in any odd place.)

I think that there is a lazy programmer's solution for getting +3.0 with 
a positive probability:

1. pick some number called epsilon, eg 0.5, smaller the better, but not 
too small
2. draw a random number r between -2d0 and (+ 3d0 epsilon) as described 
earlier in the thread,
3. if (< 3d0 r), then discard and go to step 2 to draw again, otherwise 
return r

The termination time for this algorithm is finite in expected value, and 
is also small in practice if you pick an epsilon small enough.

HTH,

Tamas
From: Pascal J. Bourguignon
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <87bq07s3y4.fsf@hubble.informatimago.com>
Kent M Pitman <······@nhplace.com> writes:
> Then again, if the range is intended to be closed on both ends, the
> floats being what they are, there is a specific least float bigger
> than 3 such that it is a perfectly useful open upper bound to solve
> the problem. :) And yes, I'm too lazy to go compute what it would be.
> (Although... hmmm... because I think the floating point epsilon
> actually varies with magnitude of the float, there might be some
> slight messiness of adjusting the range by addition/subtraction.  I
> didn't think hard on this matter and whether it leaves a gap in any
> odd place.)

But in CL it's easy enough to deal with it:

(defun +epsilon (float)
  (let ((epsilon))
    (multiple-value-bind (significand exponent sign) (decode-float float)
      (* sign (scale-float (+ significand  (etypecase float
                                             (long-float   long-float-epsilon)
                                             (double-float double-float-epsilon)
                                             (single-float single-float-epsilon)
                                             (short-float  short-float-epsilon)))
                           exponent)))))

#+clisp (setf (EXT:LONG-FLOAT-DIGITS) 120)
(mapcar '+epsilon '(3.0s0 3.0f0 3.0d0 3.0l0
              3.0s26 3.0f26 3.0d26 3.0l26))
--> (3.00003s0
     3.0000002
     3.0000000000000004d0
     3.00000000000000000000000000000000000001L0
     3.00002s26
     3.0000003E26
     3.0000000000000003d26
     3.00000000000000000000000000000000000001L26)


C/USER[232]> (funcall (compile nil (lambda () 
               (loop with limit = (+epsilon 3.0) count 1 until (= 3.0 (random limit))))))
8653534

So p(3.0) ~= 1/8653534  ;-)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
        Un chat errant
se soulage
        dans le jardin d'hiver
                                        Shiki
From: Pascal J. Bourguignon
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <7c4p5zhb5c.fsf@pbourguignon.anevia.com>
"John Thingstad" <·······@online.no> writes:

> P� Tue, 05 Aug 2008 01:56:42 +0200, skrev Don Geddis <···@geddis.org>:
>
>> stassats <········@gmail.com> wrote on Sat, 2 Aug 2008 :
>>> On Aug 3, 7:21�am, h2s <··········@gmail.com> wrote:
>>>> i would like to generate random numbers in the range -2.0 to
>>>> +3.0.please help.thanks in advance
>>> (- (random 5.0) 2.0)
>>
>> How often will that code return the result +3.0?
>>
>
> Think of it -2.0 to 3.0  has a infinite number of values. Look up
> Hilbert  hotel in Wikipedia.
> So you would expect "never". Of course no random generator is perfect.
> Like Pascal said a scaling of the range 1.0 to 5.0 followed by a
> transform  to -2.0 would be the obvious approach.
> If you "need" more granularity take a fixed integer rage say 0..100
> onto  this range. This WOULD kiss the limits.

Well this would be ok for 2.5, but the upper bound is special in that
clhs RANDOM specifies that it is not reached.  The example might be
slightly misleading, since it gives a weaker condition, but the post
condition of RANDOM is:

(let ((result (random limit)))  (and (<= 0 result) (< result limit)))

Moreover, we are not dealing with real real numbers here, but with
floating point numbers.  One interesting question would be whether the
equiprobability is over the different floating point numbers or over
the corresponding mathematical ranges (it's the later, for a sane
implementation, I would hope).  But in any case, there's a finite
number of floating point numbers between 0 (inclusive) and limit
(excluded), so they all have a finite probability of being returned.

Another good question would be how to make a random generator that
would return (with equiprobability) floating point numbers between two
extrema inclusively.

-- 
__Pascal Bourguignon__
From: Raymond Toy
Subject: Re: Random numbers between -2.0 and +3.0
Date: 
Message-ID: <sxd7iav87jq.fsf@rtp.ericsson.se>
>>>>> "Pascal" == Pascal J Bourguignon <···@informatimago.com> writes:

    Pascal> Another good question would be how to make a random generator that
    Pascal> would return (with equiprobability) floating point numbers between two
    Pascal> extrema inclusively.

This link, provided by ············@gmail.com in a different thread,
explains how to generate uniform floating point numbers between 0 and
1 that cover all of the bits in a floating-point number.

http://www.cse.cuhk.edu.hk/~phwl/mt/public/archives/papers/grng_acmcs07.pdf

I think there are some mistakes in the pseudo code. Either that or I
don't understand notation.   But I think the idea is pretty
straightforward.  Basically, keep generating uniform 32-bit (or other)
size integers until you have enough significant bits to fill up a
float.   Also keep track of how many leading 0 bits there are in the
integer; this becomes the floating-point exponent.

Ray