Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: pretty easy thing but too hard for me! (random array element)




----- Original Message ----- From: "fREW" <frioux@xxxxxxxxx>
To: "Zsh users list" <zsh-users@xxxxxxxxxx>
Sent: Wednesday, August 01, 2007 5:23 PM
Subject: pretty easy thing but too hard for me! (random array element)


Hello zsh amigos!

I want to make a tiny fuction that will

#1 get a list of files
#2 choose a random file
#3 open the random file

I was going to just do something like

arr=( * )

and then

feh $arr[(some random number or something)]

but I don't know how to do the random number part and there may be a
better way anyway.  Thoughts?  Ideas?  Wisdom?

--
-fREW

Q: Why is this email 5 sentences or less?
A: http://five.sentenc.es


I have a random splash thing where as part of my companies software the start script flashes an ascii splash logo for a half-second on start-up. I got bored one day, found a ascii art font generator thing on line that let me plug in our name and get large ascii art renditions of it in about 30 differentr fonts.

I did something cruder even than loading up an array. It requires the files have consecutive numerical filenames and just applies $RANDOM and a little math directly.
But it works in bash/ksh/zsh and doesn't require zsh or a zsh module
I wouldn't be surprised if both uses of backticks are avoidable somehow.
Since even my SCO boxes all have zsh by now redoing this with the randline thing has appeal.

-----
#!/bin/sh
# Select a splash at random and display it.
# requires bash/ksh/zsh
# 20060411 brian@xxxxxxxxx
# put as many splash files as you want in /u/aljex/splash
# use only 3-digit, zero padded, numerical, consecutive filenames
# starting with 000

SplashDir=/u/aljex/splash

cd $SplashDir || exit 1
typeset -i SplashQty=`echo * |wc -w`
typeset -i RandomDivider=$((32767/$SplashQty))

RandomSelection=`printf "%03d" $((RANDOM/$RandomDivider))`

# The math is slightly off
# .1% of the time RandomSelection == SplashQty, which is 1 too high.
# Never comes out below 0 but what the heck, might as well catch that too.
[ $RandomSelection -lt 000 ] && RandomSelection=000
[ $RandomSelection -ge $SplashQty ] && RandomSelection=$((SplashQty-1))

echo
cat $RandomSelection
echo
sleep ${1:-.5}
-----


Brian K. White    brian@xxxxxxxxx    http://www.myspace.com/KEYofR
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro  BBx    Linux  SCO  FreeBSD    #callahans  Satriani  Filk!



Messages sorted by: Reverse Date, Date, Thread, Author