l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2001 Dec 30 17:03

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] bash question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] bash question

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.


LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.