[Tutor] Random numbers

Terry Carroll carroll at tjc.com
Thu Oct 9 23:40:55 EDT 2003


On Sun, 5 Oct 2003, UCT Student - DLLBIA001 wrote:

> i was just wandring if you could not help me to generate 25 random
> numbers in the range of 0 to 24 without any number repeating.
> if i were then to put this numbers onto a grid with 25 squares how would
> i go about it. the grid and squares have already been made but how to
> place the numbers i am uncertain of.

For only 25 numbers, my approach would be:

 1. create a list of numbers from 0 to 24.
 2. set up your grid with 25 spaces.
 3. get a random number between 0 and the length of the list minus 1 (24 
    when you start);
 4. pull that entry out of the list with pop() and put it into the first 
    open entry in the grid.  pop() will remove that entry from the list, 
    so the number of entries will be 24 after the first pass, 23 after the 
    second, etc.
 5. Repeat 3-4 until the list is empty.

Things to read up on:

 pop()
 random.uniform() (to generate a random number in a particular range)
 len() (to get the length of the list)


-- 
Terry Carroll
Santa Clara, CA
carroll at tjc.com 




More information about the Tutor mailing list