random number

Michael Poeltl michael.poeltl at univie.ac.at
Mon Mar 26 11:24:03 CEST 2012


* Nikhil Verma <varma.nikhil22 at gmail.com> [2012-03-26 08:49]:
> Hi
> 
> I want something to achieve like this :-
> 
> def random_number(id): # I am passing it from request
>     # do something
>     return random_number
> 
> Output
> 
> random_number(5)
> AXR670
> 
> One input that is a number in return you are getting 6 digit alphanumeric
> string.
> 
> I tried this
> s = '%06d' % random.randint(0, 999999)
> 
> it gives : '192862' (a string )
> 
> Thanks in advance.
ah - so I misunderstood - I thought you want a permutation of a given
6-digit number

It's still not quite clear to me what role 'id' is playing ... so let's
check this one;
and Steven, who is maybe more experienced than I am will help us ufrther

>>> import random, string
>>> def random_number(id):
...     characters = list(string.ascii_lowercase +
...                       string.ascii_uppercase +
...                       string.digits)
...     coll_rand = []
...     for i in range(6):
...         random.shuffle(characters)
...         coll_rand.append(characters[0])
...     return ''.join(coll_rand)
... 
>>> id = 5
>>> print (random_number(id))
puMHCr
>>>

regards
Michael


> 
> On Mon, Mar 26, 2012 at 12:10 PM, Michael Poeltl <
> michael.poeltl at univie.ac.at> wrote:
> 
> > * Nikhil Verma <varma.nikhil22 at gmail.com> [2012-03-26 08:09]:
> > > Hi All
> > >
> > > How can we generate a 6 digit random number from a given number ?
> > what about this?
> >
> > >>> given_number=123456
> > >>> def rand_given_number(x):
> > ...     s = list(str(x))
> > ...     random.shuffle(s)
> > ...     return int(''.join(s))
> > ...
> > >>> print (rand_given_number(given_number))
> > 653421
> >
> 
> 
> 
> -- 
> Regards
> Nikhil Verma
> +91-958-273-3156


-- 
Michael Poeltl
Computational Materials Physics      voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12         fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
-------------------------------------------------------------------------------
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
-------------------------------------------------------------------------------



More information about the Python-list mailing list