The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-28-2006
Registered User
 

Join Date: Jul 2005
Posts: 37
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!
Reply With Quote
Forum Sponsor
  #2  
Old 05-29-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
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
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. )

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.
Reply With Quote
  #3  
Old 05-29-2006
Registered User
 

Join Date: Jul 2005
Posts: 37
thanks

...it's 50 picks at a time, but repeat 1000 times, like re-shaffle and re-pick and so on.
Reply With Quote
  #4  
Old 05-30-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
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-
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0