[Tutor] Suggestions about documenting a function

Luke Paireepinart rabidpoobear at gmail.com
Tue Oct 31 02:48:43 CET 2006

Jorge Azedo wrote:
> I'm writing a function that generates a list with user defined length, 
> that consists only of non-repeating random integers in a user defined range.
> Even I don't understand what I just wrote, so I'm just going to post the 
> code and an example of the results :P
> ## Test module
> ## Last revision 31 October 2006
> import random
> def randomList(n1,n2,len):
>     '''n1-n2 range of numbers, len - length of the final list
>     Returns a list'''
>     i=0
>     rndlist=[]
>     if len > n2:
>         return 'The length of the list must be greater than n2'
>     else:
>         while i < len:
>             rand=random.randint(n1,n2)
>             if rndlist.count(rand) == 0:
>                 rndlist.append(rand)
>                 i=i+1
>     return rndlist
It doesn't seem like this code does what it should, does it?
I mean, if you did randomList(9,9,9)
wouldn't it go into an infinite loop?
And why does the length have to be greater than n2? that doesn't make sense.
What if I want to have 5 random numbers from within the 10 to 100 range?
randomList(10,100,5) wouldn't work.
Does this function really meet the requirements you need?

More information about the Tutor mailing list