Nemo's bash challenge for the day
Michael Still
mikal at stillhq.com
Fri Mar 29 10:14:17 EST 2002
Well, I said it could be done...
The brief: Generate a random number, and then return that element from a
list of elements, in bash
The code: (Assuming that the arguements on the command line are the
possible return options, and that the random number generator is running
as a separate script)
LOBOUND=0
HIBOUND=$#
shift $(( $LOBOUND + ($HIBOUND * $RANDOM) / (32767 + 1) ))
echo $1
See the attachments for some exploratory scripts I wrote while coming up
with this truncated sh. There are 54 lines of comments / white spaces, to
the 4 or so lines of actual code.
Have a nice life...
Cheers,
Mikal
PS: How good is the bash random? See the attachment output.count for a
summary of 100,000 numbers between 1 and 10 being generated with the
default seed. It's probably good enough for most people.
PPS: If you want the code, it should also be online at
http://www.stillhq.com/cgi-bin/getpage?area=bashrand&page=index.htm in
about 30 minutes, depending when Andrew goes to sleep.
--
Michael Still (mikal at stillhq.com) UMT+11hrs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: random.sh
Type: application/x-sh
Size: 1536 bytes
Desc:
Url : http://lists.samba.org/archive/linux/attachments/20020329/8d8b77d1/random.sh
-------------- next part --------------
A non-text attachment was scrubbed...
Name: listitem.sh
Type: application/x-sh
Size: 297 bytes
Desc:
Url : http://lists.samba.org/archive/linux/attachments/20020329/8d8b77d1/listitem.sh
-------------- next part --------------
1: 9964
2: 10009
3: 9978
4: 10015
5: 9997
6: 10020
7: 10011
8: 10008
9: 10006
10: 9992
More information about the linux
mailing list