![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to generate a 'kill' list | victorcheung | UNIX for Advanced & Expert Users | 10 | 05-14-2008 11:04 PM |
| generate random number in perl | zx1106 | Shell Programming and Scripting | 2 | 03-17-2008 09:13 PM |
| Generate a random password | chiru_h | Shell Programming and Scripting | 5 | 10-07-2007 05:03 PM |
| how to generate a list of files | jasongr | Shell Programming and Scripting | 3 | 12-13-2005 05:15 AM |
| How to generate a random number? | MacMonster | High Level Programming | 2 | 10-15-2001 09:35 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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:
Code:
#! /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
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. |
|
#3
|
|||
|
|||
|
thanks
...it's 50 picks at a time, but repeat 1000 times, like re-shaffle and re-pick and so on.
|
|
#4
|
||||
|
||||
|
Try something like this
Code:
#! /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-
|
||||
| Google The UNIX and Linux Forums |