Whrandom

Ole Martin Bjorndalen olemb at stud.cs.uit.no
Tue May 15 15:54:36 CEST 2001


Duncan Smith wrote:
> 
> I would do the following.
> 
> >>> import whrandom
> >>> dict = {}
> >>> while len(dict.keys()) != 5:
> ...  dict[whrandom.randint(1, 1000)] = None
> ...
> >>> dict.keys()
> [247, 844, 578, 892, 244]

Neat. A slightly faster version is:

>>> import random
>>> dict = {}
>>> while len(dict) != 5:
...     dict[random.randint(1, 1001)] = None
... 
>>> dict.keys()
[535, 859, 786, 17, 147]

(I've also replaced the depreciated whrandom with
random and bumped up randint() arguments as suggested
by others in this thread.)


-- 
   Ole Martin Bjoerndalen
 http://www.cs.uit.no/~olemb/ 
    olemb at stud.cs.uit.no



More information about the Python-list mailing list