And then Nemo wanted weighted random numbers

  • Post author:
  • Post category:Bashrand

Nemo then wanted weighted random numbers, so this item has been added to this page. The following script selects a random element from a weight list of options... #!/bin/bash # Copyright (c) Michael Still 2002 # Released under the terms of the GNU GPL # In this case, Nemo wants to be able to specify a list of items, with # weights associated with them... # $1 is the list with weights, in the form: # "1 frog 2 banana 3 hamster" # Scary assumption number one, people hand me correctly formatted lists # Incidentally, this will break with numbers exist in the items I am handed # e.g. Banana42 will break this NUMBERS=`echo $1 | sed 's/[^0-9 ]//g'` WORDS=`echo $1 | sed 's/[0-9]//g'` WEIGHTED="" # Build the list of options, including the weights for NUM in $NUMBERS do WORD=`echo $WORDS | sed 's/ .*$//'` WORDS=`echo $WORDS | sed "s/^$WORD *//"` COUNT=0 while [ $COUNT -lt $NUM ] do WEIGHTED=`echo "$WEIGHTED $WORD"` COUNT=$(( $COUNT + 1 )) done done # Get the random number LOBOUND=1 HIBOUND=`echo $WEIGHTED | wc -w` RANDMAX=32767 BINUMBER=$(( $LOBOUND + ($HIBOUND * $RANDOM) / ($RANDMAX + 1) )) # Get the item -- I can't use…

Continue ReadingAnd then Nemo wanted weighted random numbers

Getting a random number in bash

  • Post author:
  • Post category:Bashrand

This script generates a bounded random number: #!/bin/bash # Generate a random number. Copyright (c) Michael Still 2002 # Released under the terms of the GNU GPL # # (Is it possible to copyright a single line of code?) # To quote from the rand manpage as to why we bound the random number this way: # # In Numerical Recipes in C: The Art of Scientific Computing # (William H. Press, Brian P. Flannery, Saul A. Teukolsky, # William T. Vetterling; New York: Cambridge University # Press, 1992 (2nd ed., p. 277)), the following comments are # made: # "If you want to generate a random integer between 1 # and 10, you should always do it by using high-order # bits, as in # # j=1+(int) (10.0*rand()/(RAND_MAX+1.0)); # # and never by anything resembling # # j=1+(rand() % 10); # # (which uses lower-order bits)." # To seed the random number generator, set RANDOM to a value... We can see # that the bash code (2.05a in this case) already does some seeding for us... # # brand () # { # rseed = rseed * 1103515245 + 12345; # return ((unsigned int)((rseed >> 16) & 32767)); /*…

Continue ReadingGetting a random number in bash

Getting an arbitary item from a list

  • Post author:
  • Post category:Bashrand

This script gets the specified element form the list on the command line... #!/bin/bash # Select a specified item from a list. Copyright (c) Michael Still 2002 # Released under the terms of the GNU GPL # $1 is the number to get, $* except for $1 is the list of options, delimited # by a space each # We can the shift operation to get to the right number shift $1 echo $1

Continue ReadingGetting an arbitary item from a list

End of content

No more pages to load