Using the Random Module.
WDC
ki4yjl at gmail.com
Sat Jul 12 12:15:06 EDT 2008
On Jul 12, 10:06 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 11 Jul 2008 12:27:32 -0700, castironpi wrote:
> > You want a random integer. Is there a range you want it in?
>
> > Past a certain point, you'll exceed the granularity of the random number
> > generator, and some values in the range will never be generated.
>
> You might want to produce an unbounded random integer, where *every*
> integer has a chance to be returned:
>
> def unbounded_randint():
> i = 0
> while True:
> if random.random() < 0.5:
> return i
> i += 1
>
> This returns 0 with probability 1/2, 1 with probability 1/4, 2 with
> probability 1/8, etc. The probability distribution is centered close to
> zero (the mode and median are both zero, and I'm too lazy to calculate
> the mean) and it has an infinitely long tail.
>
> --
> Steven
Thanks Steven. I like how that bit works.
No range in particular. I was just wondering if there was a better way
than what I had been doing. Thanks for help.
More information about the Python-list
mailing list