Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > generate random digits with length of 5

Reply
Thread Tools

generate random digits with length of 5

 
 
sotirac
Guest
Posts: n/a
 
      09-28-2008
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.


<pre>
random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
elements
code = 'this is a string' + str(random_number[0]) +
str(random_number[1]) + str(random_number[2]) + str(random_number[3])
+ str(random_number[4])
</pre>

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      09-28-2008
On Sun, Sep 28, 2008 at 12:59 PM, sotirac <(E-Mail Removed)> 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.
>
>
> <pre>
> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
> elements
> code = 'this is a string' + str(random_number[0]) +
> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
> + str(random_number[4])


code = ''.join(str(digit) for digit in random_number)

Regards,
Chris

> </pre>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
 
 
 
Aaron \Castironpi\ Brady
Guest
Posts: n/a
 
      09-28-2008
On Sep 28, 2:59*pm, sotirac <(E-Mail Removed)> 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.
>
> <pre>
> *random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
> elements
> *code = 'this is a string' + str(random_number[0]) +
> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
> + str(random_number[4])
> </pre>


'%05i'%random.randint(0,99999)
 
Reply With Quote
 
Gary M. Josack
Guest
Posts: n/a
 
      09-28-2008
Chris Rebert wrote:
> On Sun, Sep 28, 2008 at 12:59 PM, sotirac <(E-Mail Removed)> 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.
>>
>>
>> <pre>
>> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
>> elements
>> code = 'this is a string' + str(random_number[0]) +
>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
>> + str(random_number[4])
>>

>
> code = ''.join(str(digit) for digit in random_number)
>
> Regards,
> Chris
>
>
>> </pre>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>

will random.randint(10000,99999) work for you?
 
Reply With Quote
 
Gary M. Josack
Guest
Posts: n/a
 
      09-28-2008
Aaron "Castironpi" Brady wrote:
> On Sep 28, 2:59 pm, sotirac <(E-Mail Removed)> 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.
>>
>> <pre>
>> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
>> elements
>> code = 'this is a string' + str(random_number[0]) +
>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
>> + str(random_number[4])
>> </pre>
>>

>
> '%05i'%random.randint(0,99999)
> --
> http://mail.python.org/mailman/listinfo/python-list
>

This produces numbers other than 5 digit numbers. making the start
number 10000 should be fine.
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      09-28-2008
sotirac:
> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5 elements


But note that's without replacement. So if you want really random
numbers you can do this:

>>> from string import digits
>>> from random import choice
>>> "".join(choice(digits) for d in xrange(5))

'93898'

If you need more speed you can invent other solutions, like (but I
don't know if it's faster):

>>> from random import shuffle
>>> ldigits = list(digits)
>>> ldigits

['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> shuffle(ldigits)
>>> ldigits

['3', '8', '6', '4', '9', '7', '5', '2', '0', '1']
>>> "".join(ldigits[:5])

'38649'

But this may be the most efficient way:

>>> from random import randrange
>>> str(randrange(100000)).zfill(5)

'37802'

Bye,
bearophile
 
Reply With Quote
 
Gary M. Josack
Guest
Posts: n/a
 
      09-28-2008
Gary M. Josack wrote:
> Aaron "Castironpi" Brady wrote:
>> On Sep 28, 2:59 pm, sotirac <(E-Mail Removed)> 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.
>>>
>>> <pre>
>>> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
>>> elements
>>> code = 'this is a string' + str(random_number[0]) +
>>> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
>>> + str(random_number[4])
>>> </pre>
>>>

>>
>> '%05i'%random.randint(0,99999)
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

> This produces numbers other than 5 digit numbers. making the start
> number 10000 should be fine.
> --
> http://mail.python.org/mailman/listinfo/python-list

nevermind. my brain is tired tonight. this is the best solution.
 
Reply With Quote
 
Mensanator
Guest
Posts: n/a
 
      09-28-2008
On Sep 28, 3:11�pm, "Gary M. Josack" <(E-Mail Removed)> wrote:
> Chris Rebert wrote:
> > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <(E-Mail Removed)> 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.

>
> >> <pre>
> >> �random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
> >> elements
> >> �code = 'this is a string' + str(random_number[0]) +
> >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
> >> + str(random_number[4])

>
> > code = ''.join(str(digit) for digit in random_number)

>
> > Regards,
> > Chris

>
> >> </pre>

>
> >> --
> >>http://mail.python.org/mailman/listinfo/python-list

>
> will random.randint(10000,99999) work for you?


It doesn't meet the OP's requirement that the number
can start with 0. Also, the method the OP asks about
returns a list of unique numbers, so no number can
be duplicated. He can get 02468 but not 13345.

Now, IF it's ok to have an arbitrary number of leading
0s, he can do this:

>>> str(random.randint(0,99999)).zfill(5)

'00089'
>>> str(random.randint(0,99999)).zfill(5)

'63782'
>>> str(random.randint(0,99999)).zfill(5)

'63613'
>>> str(random.randint(0,99999)).zfill(5)

'22315'
 
Reply With Quote
 
Aaron \Castironpi\ Brady
Guest
Posts: n/a
 
      09-28-2008
On Sep 28, 3:44*pm, Mensanator <(E-Mail Removed)> wrote:
> On Sep 28, 3:11 pm, "Gary M. Josack" <(E-Mail Removed)> wrote:
>
>
>
> > Chris Rebert wrote:
> > > On Sun, Sep 28, 2008 at 12:59 PM, sotirac <(E-Mail Removed)> 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.

>
> > >> <pre>
> > >> random_number = random.sample([0,1,2,3,4,5,6,7,8,9], 5) # choose 5
> > >> elements
> > >> code = 'this is a string' + str(random_number[0]) +
> > >> str(random_number[1]) + str(random_number[2]) + str(random_number[3])
> > >> + str(random_number[4])

>
> > > code = ''.join(str(digit) for digit in random_number)

>
> > > Regards,
> > > Chris

>
> > >> </pre>

>
> > >> --
> > >>http://mail.python.org/mailman/listinfo/python-list

>
> > will random.randint(10000,99999) work for you?

>
> It doesn't meet the OP's requirement that the number
> can start with 0. Also, the method the OP asks about
> returns a list of unique numbers, so no number can
> be duplicated. He can get 02468 but not 13345.
>
> Now, IF it's ok to have an arbitrary number of leading
> 0s, he can do this:
>
> >>> str(random.randint(0,99999)).zfill(5)

> '00089'
> >>> str(random.randint(0,99999)).zfill(5)

> '63782'
> >>> str(random.randint(0,99999)).zfill(5)

> '63613'
> >>> str(random.randint(0,99999)).zfill(5)

>
> '22315'


Is a while loop until there are 5 distinct digits best otherwise?

while 1:
a= '%05i'% random.randint( 0, 99999 )
if len( set( a ) )== 5: break

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-28-2008
> 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.

-tkc



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate random string matching specific pattern and length Kevin Ruby 7 05-17-2011 02:47 AM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Length of longest contiguous digits exercise Csaba Gabor Javascript 19 10-26-2009 01:37 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM



Advertisments