Whrandom

Joshua Marshall jmarshal at mathworks.com
Tue May 15 10:28:37 EDT 2001


Ole Martin Bjorndalen <olemb at stud.cs.uit.no> wrote:
> 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.)

Note that random.randint is also deprecated in favor of randrange.  In
this case randint(1, 1000) was correct, and corresponds to
randrange(1, 1001).

The deprecation of randint was a good idea, in my opinion.  I'd often
forget the randint range includes its second argument.



More information about the Python-list mailing list