Subdomain Posts
None | 21 days ago
None | 21 days ago
None | 303 days ago
Bash | 483 days ago
Bash | 501 days ago
None | 511 days ago
Bash | 511 days ago
Bash | 511 days ago
Bash | 518 days ago
None | 531 days ago
Recent Posts
None | 21 sec ago
None | 27 sec ago
None | 30 sec ago
OCaml | 43 sec ago
PHP | 54 sec ago
None | 1 min ago
ASM (NASM) | 1 min ago
None | 1 min ago
None | 2 min ago
None | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By MulX on the 22nd of Nov 2008 01:51:20 PM Download | Raw | Embed | Report
  1. #!/bin/bash
  2. # random-between.sh
  3. # Random number between two specified values.
  4. # Script by Bill Gradwohl, with minor modifications by the document author.
  5. # minor change by MulX
  6. # Used with permission.
  7.  
  8.  
  9. randomBetween() {
  10.    #  Generates a positive or negative random number
  11.    #+ between $min and $max
  12.    #+ and divisible by $divisibleBy.
  13.    #  Gives a "reasonably random" distribution of return values.
  14.    #
  15.    #  Bill Gradwohl - Oct 1, 2003
  16.  
  17.    syntax() {
  18.    # Function embedded within function.
  19.       echo
  20.       echo    "Syntax: randomBetween [min] [max] [multiple]"
  21.       echo
  22.       echo -n "Expects up to 3 passed parameters, "
  23.       echo    "but all are completely optional."
  24.       echo    "min is the minimum value"
  25.       echo    "max is the maximum value"
  26.       echo -n "multiple specifies that the answer must be "
  27.       echo     "a multiple of this value."
  28.       echo    "    i.e. answer must be evenly divisible by this number."
  29.       echo    
  30.       echo    "If any value is missing, defaults area supplied as: 0 32767 1"
  31.       echo -n "Successful completion returns 0, "
  32.       echo     "unsuccessful completion returns"
  33.       echo    "function syntax and 1."
  34.       echo -n "The answer is returned in the global variable "
  35.       echo    "randomBetweenAnswer"
  36.       echo -n "Negative values for any passed parameter are "
  37.       echo    "handled correctly."
  38.    }
  39.  
  40.    local min=${1:-0}
  41.    local max=${2:-32767}
  42.    local divisibleBy=${3:-1}
  43.    # Default values assigned, in case parameters not passed to function.
  44.  
  45.    local x
  46.    local spread
  47.  
  48.    # Let's make sure the divisibleBy value is positive.
  49.    [ ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy))
  50.  
  51.    # Sanity check.
  52.    if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o  ${min} -eq ${max} ]; then
  53.       syntax
  54.       return 1
  55.    fi
  56.  
  57.    # See if the min and max are reversed.
  58.    if [ ${min} -gt ${max} ]; then
  59.       # Swap them.
  60.       x=${min}
  61.       min=${max}
  62.       max=${x}
  63.    fi
  64.  
  65.    #  If min is itself not evenly divisible by $divisibleBy,
  66.    #+ then fix the min to be within range.
  67.    if [ $((min/divisibleBy*divisibleBy)) -ne ${min} ]; then
  68.       if [ ${min} -lt 0 ]; then
  69.          min=$((min/divisibleBy*divisibleBy))
  70.       else
  71.          min=$((((min/divisibleBy)+1)*divisibleBy))
  72.       fi
  73.    fi
  74.  
  75.    #  If max is itself not evenly divisible by $divisibleBy,
  76.    #+ then fix the max to be within range.
  77.    if [ $((max/divisibleBy*divisibleBy)) -ne ${max} ]; then
  78.       if [ ${max} -lt 0 ]; then
  79.          max=$((((max/divisibleBy)-1)*divisibleBy))
  80.       else
  81.          max=$((max/divisibleBy*divisibleBy))
  82.       fi
  83.    fi
  84.  
  85.    #  ---------------------------------------------------------------------
  86.    #  Now, to do the real work.
  87.  
  88.    #  Note that to get a proper distribution for the end points,
  89.    #+ the range of random values has to be allowed to go between
  90.    #+ 0 and abs(max-min)+divisibleBy, not just abs(max-min)+1.
  91.  
  92.    #  The slight increase will produce the proper distribution for the
  93.    #+ end points.
  94.  
  95.    #  Changing the formula to use abs(max-min)+1 will still produce
  96.    #+ correct answers, but the randomness of those answers is faulty in
  97.    #+ that the number of times the end points ($min and $max) are returned
  98.    #+ is considerably lower than when the correct formula is used.
  99.    #  ---------------------------------------------------------------------
  100.  
  101.    spread=$((max-min))
  102.    #  Omair Eshkenazi points out that this test is unnecessary,
  103.    #+ since max and min have already been switched around.
  104.    [ ${spread} -lt 0 ] && spread=$((0-spread))
  105.    let spread+=divisibleBy
  106.    randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+min))  
  107.  
  108.    return 0
  109.  
  110.    #  However, Paulo Marcel Coelho Aragao points out that
  111.    #+ when $max and $min are not divisible by $divisibleBy,
  112.    #+ the formula fails.
  113.    #
  114.    #  He suggests instead the following formula:
  115.    #    rnumber = $(((RANDOM%(max-min+1)+min)/divisibleBy*divisibleBy))
  116.  
  117. }
  118.  
  119. # Let's test the function.
  120. randomBetween 1 23 1
  121.  
  122. # Let's check the results
  123. echo "value is : $randomBetweenAnswer"
  124.  
  125.  
  126. exit 0
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: