generate random digits with length of 5
Mensanator
mensanator at aol.com
Mon Sep 29 00:11:49 CEST 2008
On Sep 28, 4:02�pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> > Wondering if there is a better way to generate string of numbers with
> > a length of 5 which also can have a 0 in the front of the number.
>
> If you want to resample the same digit multiple times, either of these
> two will do:
>
> �>>> from random import choice
> �>>> ''.join(str(choice(range(10))) for _ in range(5))
> '06082'
>
> �>>> from string import digits
> �>>> ''.join(choice(digits) for _ in range(5))
> '09355'
>
> If you need to prevent the digits from being reused
>
> �>>> d = list(digits)
> �>>> random.shuffle(digit)
> �>>> ''.join(d[:5])
> '03195'
>
> I suspect that the zfill responses don't have the property of equally
> distributed "randomness", as the first digit may more likely be a zero.
> � The methods here should give equal probabilities for each choice in
> each place.
Does he want equal probabilities of each digit in each place?
>
> -tkc
More information about the Python-list
mailing list