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

2009-06-14 Thread Lawrence D'Oliveiro
In message qoteitso8ru@ruuvi.it.helsinki.fi, 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

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

2009-06-14 Thread Lawrence D'Oliveiro
In message mailman.1366.1244592006.8015.python-l...@python.org, 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

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

2009-06-14 Thread Lawrence D'Oliveiro
In message mailman.1368.1244607807.8015.python-l...@python.org, 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)

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

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

2009-06-10 Thread Mensanator
On Jun 9, 11:28�pm, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 21:04:49 -0700, Mensanator wrote: On Jun 9, 8:28 pm, John Yeung gallium.arsen...@gmail.com wrote: On Jun 9, 8:45 pm, Mensanator mensana...@aol.com wrote: On Jun 9, 6:05 pm, Gabriel

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

2009-06-10 Thread John Yeung
On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au 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,

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

2009-06-10 Thread Virgil Stokes
John Yeung wrote: On Jun 10, 1:52am, Steven D'Aprano ste...@remove.this.cybersource.com.au 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

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 Lorenzo Gatti
On 10 Giu, 06:23, Esmail ebo...@hotmail.com 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

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

2009-06-10 Thread alex23
On Jun 10, 3:24 pm, John Yeung gallium.arsen...@gmail.com 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

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 Yeunggallium.arsen...@gmail.com 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

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 Mark Dickinson
On Jun 10, 7:25 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au 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

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

2009-06-10 Thread dickinsm
Esmail ebo...@hotmail.com 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

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

2009-06-10 Thread Paul McGuire
On Jun 9, 11:23 pm, Esmail ebo...@hotmail.com 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

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

2009-06-10 Thread Mensanator
On Jun 10, 4:01 am, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 7:25 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me

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

2009-06-10 Thread Mark Dickinson
On Jun 10, 6:21 pm, Mensanator mensana...@aol.com 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

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

2009-06-10 Thread Mensanator
On Jun 10, 12:37 pm, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 6:21 pm, Mensanator mensana...@aol.com 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.

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 Kernrobert.k...@gmail.com wrote: On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmailebo...@hotmail.comwrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to

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 Mark Dickinson
On Jun 10, 6:57 pm, Mensanator mensana...@aol.com wrote: On Jun 10, 12:37 pm, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 6:21 pm, Mensanator mensana...@aol.com wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it

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 in

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

2009-06-10 Thread Mark Dickinson
On Jun 10, 8:15 pm, Robert Kern robert.k...@gmail.com 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

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 Kernrobert.k...@gmail.com 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

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

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

2009-06-10 Thread Mark Dickinson
Robert Kern robert.k...@gmail.com wrote: On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kernrobert.k...@gmail.com 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 Robert Kern
On 2009-06-10 15:54, Mark Dickinson wrote: Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my

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

2009-06-10 Thread Carl Banks
On Jun 9, 2:33 pm, Esmail ebo...@hotmail.com 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

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

2009-06-10 Thread Mark Dickinson
Robert Kern robert.k...@gmail.com 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

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 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-09 Thread Gabriel Genellina
En Tue, 09 Jun 2009 18:33:39 -0300, Esmail ebo...@hotmail.com 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

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

2009-06-09 Thread Mensanator
On Jun 9, 4:33 pm, Esmail ebo...@hotmail.com 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

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, Esmailebo...@hotmail.com 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

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 Esmail
Gabriel Genellina wrote: En Tue, 09 Jun 2009 18:33:39 -0300, Esmail ebo...@hotmail.com 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 -

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, Esmailebo...@hotmail.com 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
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 Mensanator
On Jun 9, 6:12 pm, Robert Kern robert.k...@gmail.com wrote: On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmailebo...@hotmail.com  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

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

2009-06-09 Thread Paul McGuire
On Jun 9, 4:33 pm, Esmail ebo...@hotmail.com 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

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

2009-06-09 Thread Mensanator
On Jun 9, 6:05 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Tue, 09 Jun 2009 18:33:39 -0300, Esmail ebo...@hotmail.com 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

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

2009-06-09 Thread John Yeung
On Jun 9, 8:45 pm, Mensanator mensana...@aol.com wrote: On Jun 9, 6:05 pm, Gabriel Genellina gagsl-...@yahoo.com.ar 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,

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

2009-06-09 Thread John Yeung
On Jun 9, 8:39 pm, Paul McGuire pt...@austin.rr.com 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

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

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

2009-06-09 Thread alex23
On Jun 10, 11:32 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 9, 8:39 pm, Paul McGuire pt...@austin.rr.com 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 Mensanator
On Jun 9, 8:28�pm, John Yeung gallium.arsen...@gmail.com wrote: On Jun 9, 8:45�pm, Mensanator mensana...@aol.com wrote: On Jun 9, 6:05�pm, Gabriel Genellina gagsl-...@yahoo.com.ar 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

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

2009-06-09 Thread AggieDan04
On Jun 9, 4:33 pm, Esmail ebo...@hotmail.com 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

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

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 gallium.arsen...@gmail.com wrote: On Jun 9, 8:45�pm, Mensanator mensana...@aol.com wrote: On Jun 9, 6:05�pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: py a+(b-a)*z b # the expression used for

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

2009-06-09 Thread John Yeung
On Jun 9, 11:24 pm, Steven D'Aprano ste...@remove.this.cybersource.com.au 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(),

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

2009-06-09 Thread John Yeung
On Jun 10, 12:01 am, alex23 wuwe...@gmail.com wrote: On Jun 10, 11:32 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 9, 8:39 pm, Paul McGuire pt...@austin.rr.com wrote: Are you trying to generate a number in the range [0,n] by multiplying a random function that returns [0,1]

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 ste...@remove.this.cybersource.com.au 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.