21.3.2012 9:19, Nikhil Verma kirjoitti:
Hi All
I want to generate a fix 6-digit random number from a function.
eg :-
def random_generator(n):
# do domething great
return '6-digit random number'
output examples
>>>random_number(6) # n is the input number that i will get from my request
>>>987657
>>>random_number(100)
>>>987647
>>>random_number(6) # if the same input comes again it would generate a
same random number
>>>987657
If the same input cannot be generated any other way to do this part
Any help would be appreciated.
It seems that you're initializing pseudorandom generator with same seed
number. So yes, sequence of generated numbers will be same.
so doing something like this:
from random import choice # Initializes from current time.
def random_digits():
return [choice('0123456789') for i in range(7)]
or if you need it:
from random import randint # Initializes from current time.
def random_int():
return randit(100000, 999999)
Note that pseudorandom generator is only initialized first time import
happens. After that you can set it manually using
random.seed([x]) function.
--
Jani Tiainen
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.