On Mon, Apr 16, 2001 at 02:14:11PM -0700, Mark K. Kim wrote:
> On Mon, 16 Apr 2001, Micah Cowan wrote:
> 
> > ------------------------
> > #!/bin/bash
> >
> > declare -a my_array
> > my_array=("$@")
> >
> > element_to_print=$(( $RANDOM * ${#my_array[@]} / 32767 ))
> >
> > echo "${my_array[$element_to_print]}"
> > ------------------------
> 
> Okay, that's just wrong.  Nobody should know that much bash programming :)
> 
> -Mark

Well, obviously, Pete's (embarrassingly obvious) solution is better,
since it doesn't require a more recent version of bash than, say, my
machines at work come with.  However, I'd point out that my solution
for the random number generator is a little better than the one Bill
was using (using the % operator is usually discouraged).  For a
solution like this, though, I doubt anyone (including me) would really
give a rat's ass.  See rand(3) for why.  The above method has a better
distribution of probability.  (But like I said, who cares?)

If you wanted to make it better, using a variable to replace the magic
number "32767" wouldn't be a bad idea.

Micah

Reply via email to