string random generator (Sorry if too stupid, but i am a beginner)
Bjorn Pettersen
BPettersen at NAREX.com
Fri Jul 20 13:09:55 EDT 2001
> From: Gustavo Vieira Goncalves Coelho Rios
>
> Hi folks!
>
> I am in need for a function that return a string of len x
> random generated, like
>
> strrnd(5)
> 'Df%d^'
>
> strrnd(2)
> '&@'
>
>
> I have the following code: The problem is that i don't known
> how to map a int to a char.
>
> import random
> def strrnd(n = 24) :
> result = []
> while n :
> n = n - 1
> result.append(random.randrange(1,255,1))
> return result
>
> How may i get what i want. (Sorry if too basic, but i am a
> python beginner).
> Another problem: This functins will be called thousands of
> time a minute, so performance is a definitive requirement.
Something along these lines should work:
import random, string
def randomWord(length):
res = ''
for i in range(length):
res += random.choice(string.letters)
return res
-- bjorn
More information about the Python-list
mailing list