Re: [Tutor] Generate 8 digit random number

2005-09-02 Thread Jacob S.
> Hey Tutors
>
> I saw a lot of responses...After analyze them I have resumed two 
> approaches
>
> 1.- Generate a random number from 0 to  and fill this number with
> zeros (Almost everyone's approach)
> 2.- Generate 8 random numbers and join them (Pietro and someone else)
>
> Which one of this is more randomic? I mean which one of these has lower
> chances to get duplicates?
> Until now I'm using approach number 2...Unless anyone has something
> against it...

Frankly,
I would probably prefer #1 because it's probably faster. (You're just 
generating one number, and calling 1or 2 functions on it as opposed to at 
least 1 random and 1 function for each digit)

Secondly, in both cases, there is the chance that you will get duplicates~~
So the best way to do that is save passwords already generated in a list.

##  Try this ###

import random
import cPickle  ## This is to save passwords already generated between 
program runs.

def getlist():
try:
fileobj = file("passwords.lst","r")
returnlist = cPickle.load(fileobj)
fileobj.close()
except IOError:
returnlist = []
return returnlist

def generate():
return str(random.randrange(0,)).zfill(8)  ## Not that this is 
the best, but it is concise

def close(li):
obj = file("passwords.lst","w")
cPickle.dump(li,obj)
obj.close()


masterlist = getlist()

num = 0
while 1:
while num in masterlist:
num = generate()
masterlist.append(num)
print num
m = raw_input()
if m == 'quit':
close(masterlist)
break
##

Okay, I believe it will work, I haven't tested it though. Any problems, 
grouch at me, please.

Respectfully,
Jacob 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Michael P. Reilly
On 8/26/05, Alberto Troiano <[EMAIL PROTECTED]> wrote:
Hi everyoneI need to generate a password..It has to be an 8 digit number and it has tobe randomThe code I've been trying is the following:import randomrandom.randrange(,)
The code works but sometimes it picks a number with 7 digits. Is there anyway that I can tell him to select always a random number with 8 digits?Thanks in advancedAlberto

Along with everyone else's solutions, I'll throw out:

  str(random.randrange()).zfill(8)

  -Arcege-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Terry Carroll
On Fri, 26 Aug 2005, Alberto Troiano wrote:

> I need to generate a password..It has to be an 8 digit number and it has
> to be random
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?

well, you've gotten a lot of good answers, so let me chime in with a 
really ugly one:

>>> ''.join(['000',str(random.randrange(,))])[-8:]
'00101381'


(I don't really recommend this, but this is pretty much akin to the
prefered way in IBM's EXEC2 language as I used to use it some 25 years
ago!)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Alan Gauld
> I need to generate a password..It has to be an 8 digit number and it
> has to be random

I assume you mean a string representing an 8 digit random number?
If so...

> import random
> random.randrange(,)
>
> The code works but sometimes it picks a number with 7 digits. Is
> there any way that I can tell him to select always a random number
> with 8 digits?

Eventually it will generate a single digit number if its truly random!

But when you convert it to a string (see assumption above!) you can
pad it with zeros


passString = "%08d" % random.randrange(0,)

Is that what you want?

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Ertl, John
Sorry for that you will have to change the %09 to a %08 to get 8 digits.   I
got a bit to fixated on the 

John Ertl  

 -Original Message-
From:   Ertl, John  
Sent:   Friday, August 26, 2005 2:23 PM
To: Alberto Troiano; tutor@python.org
Subject:RE: [Tutor] Generate 8 digit random number

Alberto

If you don't mind having leading 0 then you could just do the random like
you did then format it to 9 digits.

You could give this a try

num = random.randrange(,)
num8 = "%09i" % num

John Ertl 


 -Original Message-
From:   Byron [mailto:[EMAIL PROTECTED] 
Sent:   Friday, August 26, 2005 1:50 PM
To: Alberto Troiano; tutor@python.org
Subject:        Re: [Tutor] Generate 8 digit random number

Hi Alberto,

Here's how to do it:

---

import random

def generateKey():
nums = "0123456789"
strNumber = ""
count = 0
while (count < 8):
strNumber += nums[random.randrange(len(nums))]
count += 1
print strNumber

# A quick test...
count = 0
while (count < 1):
generateKey()
count += 1


---

Byron  :-)

---


Alberto Troiano wrote:
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has
to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any

> way that I can tell him to select always a random number with 8 digits?
> 
> Thanks in advanced
> 
> Alberto
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Ertl, John
Alberto

If you don't mind having leading 0 then you could just do the random like
you did then format it to 9 digits.

You could give this a try

num = random.randrange(,)
num8 = "%09i" % num

John Ertl 


 -Original Message-
From:   Byron [mailto:[EMAIL PROTECTED] 
Sent:   Friday, August 26, 2005 1:50 PM
To: Alberto Troiano; tutor@python.org
Subject:        Re: [Tutor] Generate 8 digit random number

Hi Alberto,

Here's how to do it:

---

import random

def generateKey():
nums = "0123456789"
strNumber = ""
count = 0
while (count < 8):
strNumber += nums[random.randrange(len(nums))]
count += 1
print strNumber

# A quick test...
count = 0
while (count < 1):
generateKey()
count += 1


---

Byron  :-)

---


Alberto Troiano wrote:
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has
to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any

> way that I can tell him to select always a random number with 8 digits?
> 
> Thanks in advanced
> 
> Alberto
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Byron
Hi Alberto,

Here's how to do it:

---

import random

def generateKey():
nums = "0123456789"
strNumber = ""
count = 0
while (count < 8):
strNumber += nums[random.randrange(len(nums))]
count += 1
print strNumber

# A quick test...
count = 0
while (count < 1):
generateKey()
count += 1


---

Byron  :-)

---


Alberto Troiano wrote:
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?
> 
> Thanks in advanced
> 
> Alberto
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Alberto Troiano
Hey ALL

The code Kent sent might work ok but I didn't see it because of what Pierre 
said about the left 0's
I think I will go with Pietro's approach. I think is neater and shorter than 
the randrange approach

Thanks to all that helped

Best Regards

Alberto


>From: Kent Johnson <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] Generate 8 digit random number
>Date: Fri, 26 Aug 2005 10:16:37 -0400
>
>Alberto Troiano wrote:
> > Hi Kent
> >
> > Nope...
> > Not workingI'm still getting 7 even 6 digits number
>
>Are you sure? It works for me:
>  >>> import random
>  >>> random.randrange(1000,)
>42247129
>  >>> for i in range(1000):
>  ...   x = random.randrange(1000,)
>  ...   if len(str(x)) < 8:
>  ... print x
>  ...
>  >>> (prints nothing - they are all 8 digits)
>
>Kent
> >
> > Any other idea?
> >
> > Thanks
> >
> > Alberto
> >
> >> From: Kent Johnson <[EMAIL PROTECTED]>
> >> CC: tutor@python.org
> >> Subject: Re: [Tutor] Generate 8 digit random number
> >> Date: Fri, 26 Aug 2005 10:00:50 -0400
> >>
> >> Alberto Troiano wrote:
> >> > Hi everyone
> >> >
> >> > I need to generate a password..It has to be an 8 digit number and it
> >> has to
> >> > be random
> >> >
> >> > The code I've been trying is the following:
> >> >
> >> >
> >> > import random
> >> > random.randrange(,)
> >> >
> >> > The code works but sometimes it picks a number with 7 digits. Is
> >> there any
> >> > way that I can tell him to select always a random number with 8 
>digits?
> >>
> >> random.randrange(1000,) ??
> >>
> >> Kent
> >>
> >> ___
> >> Tutor maillist  -  Tutor@python.org
> >> http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> >
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Kent Johnson
Alberto Troiano wrote:
> Hi Kent
> 
> Nope...
> Not workingI'm still getting 7 even 6 digits number

Are you sure? It works for me:
 >>> import random
 >>> random.randrange(1000,)
42247129
 >>> for i in range(1000):
 ...   x = random.randrange(1000,)
 ...   if len(str(x)) < 8:
 ... print x
 ...
 >>> (prints nothing - they are all 8 digits)

Kent
> 
> Any other idea?
> 
> Thanks
> 
> Alberto
> 
>> From: Kent Johnson <[EMAIL PROTECTED]>
>> CC: tutor@python.org
>> Subject: Re: [Tutor] Generate 8 digit random number
>> Date: Fri, 26 Aug 2005 10:00:50 -0400
>>
>> Alberto Troiano wrote:
>> > Hi everyone
>> >
>> > I need to generate a password..It has to be an 8 digit number and it 
>> has to
>> > be random
>> >
>> > The code I've been trying is the following:
>> >
>> >
>> > import random
>> > random.randrange(,)
>> >
>> > The code works but sometimes it picks a number with 7 digits. Is 
>> there any
>> > way that I can tell him to select always a random number with 8 digits?
>>
>> random.randrange(1000,) ??
>>
>> Kent
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Pietro Ciuffo
On Fri, Aug 26, 2005 at 01:50:04PM +, Alberto Troiano wrote:
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?
> 
> Thanks in advanced

Hi,
The following code seeems to work.

from random import choice
lnd='0123456789'
print ''.join(map(lambda x,y=lnd: choice(y), range(8)))

Bye

-- 
The Old Man and the Sea LITE(tm) -- by Ernest Hemingway
'An old man goes fishing, but doesn't have much luck.'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Alberto Troiano
Hi Kent

Nope...
Not workingI'm still getting 7 even 6 digits number

Any other idea?

Thanks

Alberto
>From: Kent Johnson <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] Generate 8 digit random number
>Date: Fri, 26 Aug 2005 10:00:50 -0400
>
>Alberto Troiano wrote:
> > Hi everyone
> >
> > I need to generate a password..It has to be an 8 digit number and it has 
>to
> > be random
> >
> > The code I've been trying is the following:
> >
> >
> > import random
> > random.randrange(,)
> >
> > The code works but sometimes it picks a number with 7 digits. Is there 
>any
> > way that I can tell him to select always a random number with 8 digits?
>
>random.randrange(1000,) ??
>
>Kent
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Pierre Barbier de Reuille
Is "0" a valid digit for your password ?

If the answer is "YES" then, remember that the "0" at the left of a
number are juste removed ! In that case, try writing your password with :

"%08d" % password

Pierre

Alberto Troiano a écrit :
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?
> 
> Thanks in advanced
> 
> Alberto
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77fax   : (33) 4 67 61 56 68
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate 8 digit random number

2005-08-26 Thread Kent Johnson
Alberto Troiano wrote:
> Hi everyone
> 
> I need to generate a password..It has to be an 8 digit number and it has to 
> be random
> 
> The code I've been trying is the following:
> 
> 
> import random
> random.randrange(,)
> 
> The code works but sometimes it picks a number with 7 digits. Is there any 
> way that I can tell him to select always a random number with 8 digits?

random.randrange(1000,) ??

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor