[Tutor] random number equations . . .
Gregor Lingl
glingl at aon.at
Thu Jun 3 18:53:27 EDT 2004
Dragonfirebane at aol.com schrieb:
> I don't want to write my own equations for creating random numbers
> (based on 'time.time()'), but i need random numbers to create account
> id's and assign starting money in the program segment below . . . i
> would use [import random *] but i'm not sure which section of random
> is a random number generator or the syntax necessary to apply it. Any
> help would be appreciated.
>
There are two or three functions in module random, which you could use, if
you didn't want to build your own:
>>> from random import randrange # works similar to range
>>> for i in range(20): print randrange(4),
1 1 0 3 2 1 3 2 0 0 1 1 3 3 0 1 1 0 0 2
>>> for i in range(20): print randrange(5,9),
5 7 8 5 8 7 8 7 7 6 6 8 5 7 7 5 8 8 5 6
>>> from random import randint
>>> for i in range(20): print randint(0,4), # needs start and
endpoint(inclusive) of interval
3 0 3 3 2 2 4 4 1 1 3 0 4 1 4 3 2 1 3 2
>>> for i in range(20): print randint(1,6), # dice
3 5 3 3 2 2 5 5 2 1 1 1 6 2 6 5 3 6 4 4
>>> from random import choice # from any sequence type, e.g. strings
>>> for i in range(20): print choice("abcdefghijklmnopqrstuvwxyz"),
p h f b a o p l n q z y k f n c k r v l
>>> for i in range(20): print choice("abcdefghijklmnopqrstuvwxyz".upper()),
V Y R I T Q K O E A S V E T F V Q M M Q
>>>
You may find docs here:
http://docs.python.org/lib/module-random.html
Regards,
Gregor
More information about the Tutor
mailing list