how to generate a random list from a given list

Dear Masters,

Is there an easy way to generate a random list from a give list of names? Let's say, I have a file containing 15000 city name of world(spreadsheet, names in the first column), I would like to randomly pick up 50 cities each time for total 1000 picks. Or doesn't anyone know a program can be used for this purpose?

Thanks!

ksh has a built-in random number generator. It's performance is not spectacular, but it is probably good enough for your purposes. It will generate random numbers in the range of 0 to 32767. You will need a different range. Use this technique:

#! /usr/bin/ksh

#
# RANDOM is a random number between 0 and 32767 (inclusive)
max_random=32768

#
# We want a random number between 0 and 14 (inclusive)
max_needed=15

i=0
while ((i<7)) ; do
        ((r=RANDOM*max_needed/max_random))
        echo $r
        ((i=i+1))
done

exit 0

Warning: do not use the modulus operation to convert the range. The above code is using the high order bits of the initial random number while the modulus operation would use the low order bits. So you would generate a random number between 0 and 14999. Then you would add one to get a line number between 1 and 15000. Then just use sed or something to retrieve that line. (Or something like that. I got lost in your math...I don't understand how picking 50 cities gives us 1000 picks. :confused: )

But this assumes that it is ok to pick the lsame line twice from the file every now and then. Many times that is exactly what you want. But a few times, duplicates are not ok. Suppose that there were 52 lines in the file representing the cards in a deck of playing cards. If you want to generate a random poker hand, you must eliminate duplicates. In this case, you would first generate a number between 1 and 52 and, as before, you would retrieve the selected line. But then you would use sed to delete that line leaving only 51 lines in the file. For your second card, you generate a random number between 1 and 51. And so on.

...it's 50 picks at a time, but repeat 1000 times, like re-shaffle and re-pick and so on.

Try something like this

#! /usr/bin/ksh
#
# Usage: $0 [file [count]]
# 

File=${1}
Count=${2:-10}

while read line
do
   echo "$RANDOM�$line"
done < $File     |  \
sort -t� -k1,1n  |  \
head -$Count     |  \
cut -d� -f2-