Re: random number including 1 - i.e. [0,1]

2009-06-14 Thread Lawrence D'Oliveiro
In message , Esmail wrote: > Here is part of the specification of an algorithm I'm implementing that > shows the reason for my original query: > > vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) > > xid = xid + vid (1b) Are the terms meant to be clamped to a parti

Re: random number including 1 - i.e. [0,1]

2009-06-14 Thread Lawrence D'Oliveiro
In message , Esmail wrote: > I'm implementing a Particle Swarm Optimizer. Depending on what paper you > read you'll see mention of required random values "between 0 and 1" > which is somewhat ambiguous. I came across one paper that specified > the range [0,1], so inclusive 1, which was the reason

Re: random number including 1 - i.e. [0,1]

2009-06-14 Thread Lawrence D'Oliveiro
In message , Jussi Piitulainen wrote: > Miles Kaufmann writes: > >> I'm curious what algorithm calls for random numbers on a closed >> interval. > > The Box-Muller transform, polar form. At least Wikipedia says so. Doesn't seem to be necessary, if I interpret the following correctly

Re: random number including 1 - i.e. [0,1]

2009-06-11 Thread Esmail
Thanks everyone, I learned more than I expected about floats :-) and got some good explanations and ideas about all of this. Esmail -- http://mail.python.org/mailman/listinfo/python-list

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Steven D'Aprano
On Wed, 10 Jun 2009 15:32:05 -0500, Robert Kern wrote: > That's a fair point. However, one issue that hasn't been brought up is > that it might be confusing to a user why random.random() returns values > in a half-open interval while random.uniform() claims a closed interval. > Even for reasonably

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-10 16:47, Mark Dickinson wrote: And of course I'm wrong. I shouldn't have said *never*, above: from random import uniform uniform(-1e308, 1e308) inf :-( Somehow this doesn't seem worth either fixing or documenting, though. Agreed. -- Robert Kern "I have come to believe that

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
Robert Kern wrote: > On 2009-06-10 15:54, Mark Dickinson wrote: >> [...] I'm not sure I'm capable of coming up with extra wording >> for the docs that won't just cause more confusion, so I'll leave that >> to someone else. > > I did make a concrete suggestion. Yes, you did. Thank you. Submitte

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Carl Banks
On Jun 9, 2:33 pm, Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > > I am implementing an algorithm and want to stay as true to the > original design specificati

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-10 15:54, Mark Dickinson wrote: Robert Kern wrote: On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kern wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
Robert Kern wrote: > On 2009-06-10 14:46, Mark Dickinson wrote: >> On Jun 10, 8:15 pm, Robert Kern wrote: >>> On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. >>> True. However, a brief note that

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-10 15:32, Robert Kern wrote: On 2009-06-10 14:46, Mark Dickinson wrote: But I don't know why it would be useful to know that endpoints *are* sometimes included, without knowing exactly when. That's a fair point. However, one issue that hasn't been brought up is that it might be co

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kern wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that "Due to floating point arithmetic, fo

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
On Jun 10, 8:15 pm, Robert Kern wrote: > On 2009-06-10 13:53, Terry Reedy wrote: > > A full technical discussion does not below in the docs, in my opinion. A > > wike article would be fine. > > True. However, a brief note that "Due to floating point arithmetic, for some > values of a and b, b may

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-10 13:53, Terry Reedy wrote: Mensanator wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. If a < x < b is true, then a <= x <= b is true. But docs say that

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
On Jun 10, 6:57 pm, Mensanator wrote: > On Jun 10, 12:37 pm, Mark Dickinson wrote: > > > On Jun 10, 6:21 pm, Mensanator wrote: > > > > So, the 2.6.2 documentation is STILL wrong. Before it implied > > > it was ALWAYS a semi-open interval, and now it says it's ALWAYS > > > a closed interval. But

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Terry Reedy
Mensanator wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. If a < x < b is true, then a <= x <= b is true. But docs say that in general end point values might happen.

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Robert Kern
On 2009-06-09 19:27, Mensanator wrote: On Jun 9, 6:12 pm, Robert Kern wrote: On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmailwrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mensanator
On Jun 10, 12:37 pm, Mark Dickinson wrote: > On Jun 10, 6:21 pm, Mensanator wrote: > > > So, the 2.6.2 documentation is STILL wrong. Before it implied > > it was ALWAYS a semi-open interval, and now it says it's ALWAYS > > a closed interval. But neither is correct. > > Exactly which bit of the 2.

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
On Jun 10, 6:21 pm, Mensanator wrote: > So, the 2.6.2 documentation is STILL wrong. Before it implied > it was ALWAYS a semi-open interval, and now it says it's ALWAYS > a closed interval. But neither is correct. Exactly which bit of the 2.6.2 documentation do you think is incorrect? The documen

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mensanator
On Jun 10, 4:01 am, Mark Dickinson wrote: > On Jun 10, 7:25 am, John Yeung wrote: > > > > > > > On Jun 10, 1:52 am, Steven D'Aprano > > > wrote: > > > On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: > > > > Therefore, to me the most up-to-date docs (which say > > > > that uniform(a, b) ret

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Paul McGuire
On Jun 9, 11:23 pm, Esmail wrote: > Here is part of the specification of an algorithm I'm implementing that > shows the reason for my original query: > > vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) > > xid = xid + vid (1b) > > where c1 and c2 are two positive con

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread dickinsm
Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > > [...] Here are three recipes, each more pedantic than the last. They all assume that Python floats are IEE

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Mark Dickinson
On Jun 10, 7:25 am, John Yeung wrote: > On Jun 10, 1:52 am, Steven D'Aprano > > wrote: > > On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: > > > Therefore, to me the most up-to-date docs (which say > > > that uniform(a, b) returns a float in the closed > > > interval [a, b]) is closer to co

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Jussi Piitulainen
Esmail writes: > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > > I am implementing an algorithm and want to stay as true to the > original design specifications as possible though I

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Andre Engels
On Wed, Jun 10, 2009 at 8:25 AM, John Yeung wrote: > That uniform(a, b) will return a random float in the semi-open > interval [a, b) for certain values of a and b; and in the closed > interval [a, b] for other values of a and b.  (Swap a and b if a > b.) > > To me, the fact that you sometimes get

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread alex23
On Jun 10, 3:24 pm, John Yeung wrote: > Alex, did you bother to read what I quoted?  Paul McGuire suggested an > alternative in case the OP was choosing integers in a roundabout way. > I was merely pointing out that Paul's solution can be more simply > achieved using a library function. My apolog

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Lorenzo Gatti
On 10 Giu, 06:23, Esmail wrote: > Here is part of the specification of an algorithm I'm implementing that > shows the reason for my original query: > > vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) > > xid = xid + vid (1b) > > where c1 and c2 are two positive const

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Jussi Piitulainen
Miles Kaufmann writes: [...] > I'm curious what algorithm calls for random numbers on a closed > interval. The Box-Muller transform, polar form. At least Wikipedia says so. -- http://mail.python.org/mailman/listinfo/python-list

Re: random number including 1 - i.e. [0,1]

2009-06-10 Thread Virgil Stokes
John Yeung wrote: On Jun 10, 1:52 am, Steven D'Aprano wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me the most up-to-date docs (which say that uniform(a, b) returns a float in the closed interval [a, b]) is closer to correct than befor

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread John Yeung
On Jun 10, 1:52 am, Steven D'Aprano wrote: > On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: > > Therefore, to me the most up-to-date docs (which say > > that uniform(a, b) returns a float in the closed > > interval [a, b]) is closer to correct than before, > > but still fails to point out t

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Mensanator
On Jun 9, 11:28�pm, Steven D'Aprano wrote: > On Tue, 09 Jun 2009 21:04:49 -0700, Mensanator wrote: > > On Jun 9, 8:28 pm, John Yeung wrote: > >> On Jun 9, 8:45 pm, Mensanator wrote: > > >> > On Jun 9, 6:05 pm, "Gabriel Genellina" > >> > wrote: > >> > > py> a+(b-a)*z < b # the expression used fo

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Dave Angel
Steven D'Aprano wrote: On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote: The docs are now... sort of correct. For some values of a and b, uniform() can never return b. Notably, I believe uniform(0, 1) is equivalent to random(), and will never return 1. However, uniform(1, 2) CAN retur

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Steven D'Aprano
On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: > On Jun 9, 11:24 pm, Steven D'Aprano > wrote: >> On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote: >> > The docs are now... sort of correct.  For some values of a and b, >> > uniform() can never return b.  Notably, I believe uniform(0, 1)

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread John Yeung
On Jun 10, 12:01 am, alex23 wrote: > On Jun 10, 11:32 am, John Yeung wrote: > > > On Jun 9, 8:39 pm, Paul McGuire wrote: > > > Are you trying to generate a number in the > > > range [0,n] by multiplying a random function that > > > returns [0,1] * n?  If so, then you want to do > > > this using:

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread John Yeung
On Jun 9, 11:24 pm, Steven D'Aprano wrote: > On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote: > > The docs are now... sort of correct.  For some values of a and b, > > uniform() can never return b.  Notably, I believe uniform(0, 1) is > > equivalent to random(), and will never return 1.  Howe

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Steven D'Aprano
On Tue, 09 Jun 2009 21:04:49 -0700, Mensanator wrote: > On Jun 9, 8:28�pm, John Yeung wrote: >> On Jun 9, 8:45�pm, Mensanator wrote: >> >> > On Jun 9, 6:05�pm, "Gabriel Genellina" >> > wrote: >> > > py> a+(b-a)*z < b # the expression used for uniform(a,b) False >> > > py> a+(b-a)*z >> > > 11.0

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Esmail
Here is part of the specification of an algorithm I'm implementing that shows the reason for my original query: vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) xid = xid + vid (1b) where c1 and c2 are two positive constants, rand() and Rand() are two random functi

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread AggieDan04
On Jun 9, 4:33 pm, Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? You could do random.uniform(0, 1.0002). Due to floating- point rounding, there are

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Mensanator
On Jun 9, 8:28�pm, John Yeung wrote: > On Jun 9, 8:45�pm, Mensanator wrote: > > > On Jun 9, 6:05�pm, "Gabriel Genellina" wrote: > > > py> a+(b-a)*z < b # the expression used for uniform(a,b) > > > False > > > py> a+(b-a)*z > > > 11.0 > > > What you do with the number after it's created is not >

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread alex23
On Jun 10, 11:32 am, John Yeung wrote: > On Jun 9, 8:39 pm, Paul McGuire wrote: > > Are you trying to generate a number in the > > range [0,n] by multiplying a random function that > > returns [0,1] * n?  If so, then you want to do > > this using: int(random.random()*(n+1))  This will > > give eq

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Steven D'Aprano
On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote: > The docs are now... sort of correct. For some values of a and b, > uniform() can never return b. Notably, I believe uniform(0, 1) is > equivalent to random(), and will never return 1. However, uniform(1, 2) > CAN return 2, if this is any i

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread John Yeung
On Jun 9, 8:39 pm, Paul McGuire wrote: > Are you trying to generate a number in the > range [0,n] by multiplying a random function that > returns [0,1] * n?  If so, then you want to do > this using: int(random.random()*(n+1))  This will > give equal chance of getting any number from 0 to n. Bette

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread John Yeung
On Jun 9, 8:45 pm, Mensanator wrote: > On Jun 9, 6:05 pm, "Gabriel Genellina" wrote: > > py> a+(b-a)*z < b # the expression used for uniform(a,b) > > False > > py> a+(b-a)*z > > 11.0 > > What you do with the number after it's created is not > random's concern. Mensanator, you missed Gabriel's po

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Mensanator
On Jun 9, 6:05 pm, "Gabriel Genellina" wrote: > En Tue, 09 Jun 2009 18:33:39 -0300, Esmail escribió: > > > random.random() will generate a random value in the range [0, 1). > > > Is there an easy way to generate random values in the range [0, 1]? > > I.e., including 1? > > I think you shouldn't w

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Paul McGuire
On Jun 9, 4:33 pm, Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > Are you trying to generate a number in the range [0,n] by multiplying a random function that

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Mensanator
On Jun 9, 6:12 pm, Robert Kern wrote: > On 2009-06-09 18:05, Mensanator wrote: > > > > > > > On Jun 9, 4:33 pm, Esmail  wrote: > >> Hi, > > >> random.random() will generate a random value in the range [0, 1). > > >> Is there an easy way to generate random values in the range [0, 1]? > >> I.e., inc

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Esmail
Miles Kaufmann wrote: I'm curious what algorithm calls for random numbers on a closed interval. I'm implementing a Particle Swarm Optimizer. Depending on what paper you read you'll see mention of required random values "between 0 and 1" which is somewhat ambiguous. I came across one paper that

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Esmail
Robert Kern wrote: On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmail wrote: That's wrong. Where did you get it? http://docs.python.org/library/random What he said :-) (thanks Robert) -- http://mail.python.org/mailman/listinfo/python-list

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Esmail
Gabriel Genellina wrote: En Tue, 09 Jun 2009 18:33:39 -0300, Esmail escribió: random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I think you shouldn't worry about that - the difference may

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Miles Kaufmann
On Jun 9, 2009, at 7:05 PM, Mensanator wrote: On Jun 9, 4:33 pm, Esmail wrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Robert Kern
On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmail wrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Mensanator
On Jun 9, 4:33 pm, Esmail wrote: > Hi, > > random.random() will generate a random value in the range [0, 1). > > Is there an easy way to generate random values in the range [0, 1]? > I.e., including 1? > > I am implementing an algorithm and want to stay as true to the > original design specificati

Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Gabriel Genellina
En Tue, 09 Jun 2009 18:33:39 -0300, Esmail escribió: random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I think you shouldn't worry about that - the difference may be as small as 2**-53, o

random number including 1 - i.e. [0,1]

2009-06-09 Thread Esmail
Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the original design specifications as possible though I suppose the difference