| 1 | #!/bin/bashscip-shell filesystem . . `/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 | # Corrections in lines 187 and 189 by Anthony Le Clezio. |
| 6 | # Used with permission. |
| 7 | |
| 8 | |
| 9 | randomBetweenscip-shell shell abs-guide 10-4 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 | syntaxscip-shell shell abs-guide 10-4 syntax().() { |
| 18 | # Function embedded within function. |
| 19 | echoscip-shell system . . echo. |
| 20 | echoscip-shell system . . echo. "Syntax: randomBetween [min] [max] [multiple]" |
| 21 | echoscip-shell system . . echo. |
| 22 | echoscip-shell system . . echo. -n "Expects up to 3 passed parameters, " |
| 23 | echoscip-shell system . . echo. "but all are completely optional." |
| 24 | echoscip-shell system . . echo. "min is the minimum value" |
| 25 | echoscip-shell system . . echo. "max is the maximum value" |
| 26 | echoscip-shell system . . echo. -n "multiple specifies that the answer must be " |
| 27 | echoscip-shell system . . echo. "a multiple of this value." |
| 28 | echoscip-shell system . . echo. " i.e. answer must be evenly divisible by this number." |
| 29 | echoscip-shell system . . echo. |
| 30 | echoscip-shell system . . echo. "If any value is missing, defaults area supplied as: 0 32767 1" |
| 31 | echoscip-shell system . . echo. -n "Successful completion returns 0, " |
| 32 | echoscip-shell system . . echo. "unsuccessful completion returns" |
| 33 | echoscip-shell system . . echo. "function syntax and 1." |
| 34 | echoscip-shell system . . echo. -n "The answer is returned in the global variable " |
| 35 | echoscip-shell system . . echo. "randomBetweenAnswer" |
| 36 | echoscip-shell system . . echo. -n "Negative values for any passed parameter are " |
| 37 | echoscip-shell system . . 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 | [scip-shell system . . `[`. ${divisibleBy} -lt 0 ] && divisibleBy=$((0-divisibleBy)) |
| 50 | |
| 51 | # Sanity check. |
| 52 | if [scip-shell system . . `[`. $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]; then |
| 53 | syntaxscip-shell shell abs-guide 10-4 syntax(). |
| 54 | return 1 |
| 55 | fi |
| 56 | |
| 57 | # See if the min and max are reversed. |
| 58 | if [scip-shell system . . `[`. ${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 [scip-shell system . . `[`. $((min/divisibleBy*divisibleBy)) -ne ${min} ]; then |
| 68 | if [scip-shell system . . `[`. ${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 [scip-shell system . . `[`. $((max/divisibleBy*divisibleBy)) -ne ${max} ]; then |
| 78 | if [scip-shell system . . `[`. ${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 | [scip-shell system . . `[`. ${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 | min=-14 |
| 121 | max=20 |
| 122 | divisibleBy=3 |
| 123 | |
| 124 | |
| 125 | # Generate an array of expected answers and check to make sure we get |
| 126 | #+ at least one of each answer if we loop long enough. |
| 127 | |
| 128 | declare -a answer |
| 129 | minimum=${min} |
| 130 | maximum=${max} |
| 131 | if [scip-shell system . . `[`. $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]; then |
| 132 | if [scip-shell system . . `[`. ${minimum} -lt 0 ]; then |
| 133 | minimum=$((minimum/divisibleBy*divisibleBy)) |
| 134 | else |
| 135 | minimum=$((((minimum/divisibleBy)+1)*divisibleBy)) |
| 136 | fi |
| 137 | fi |
| 138 | |
| 139 | |
| 140 | # If max is itself not evenly divisible by $divisibleBy, |
| 141 | #+ then fix the max to be within range. |
| 142 | |
| 143 | if [scip-shell system . . `[`. $((maximum/divisibleBy*divisibleBy)) -ne ${maximum} ]; then |
| 144 | if [scip-shell system . . `[`. ${maximum} -lt 0 ]; then |
| 145 | maximum=$((((maximum/divisibleBy)-1)*divisibleBy)) |
| 146 | else |
| 147 | maximum=$((maximum/divisibleBy*divisibleBy)) |
| 148 | fi |
| 149 | fi |
| 150 | |
| 151 | |
| 152 | # We need to generate only positive array subscripts, |
| 153 | #+ so we need a displacement that that will guarantee |
| 154 | #+ positive results. |
| 155 | |
| 156 | disp=$((0-minimum)) |
| 157 | for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do |
| 158 | answer[i+disp]=0 |
| 159 | done |
| 160 | |
| 161 | |
| 162 | # Now loop a large number of times to see what we get. |
| 163 | loopIt=1000 # The script author suggests 100000, |
| 164 | #+ but that takes a good long while. |
| 165 | |
| 166 | for ((i=0; i<${loopIt}; ++i)); do |
| 167 | |
| 168 | # Note that we are specifying min and max in reversed order here to |
| 169 | #+ make the function correct for this case. |
| 170 | |
| 171 | randomBetween ${max} ${min} ${divisibleBy} |
| 172 | |
| 173 | # Report an error if an answer is unexpected. |
| 174 | [ ${randomBetweenAnswer} -lt ${min} -o ${randomBetweenAnswer} -gt ${max} ] \ |
| 175 | && echo MIN or MAX error - ${randomBetweenAnswer}! |
| 176 | [ $((randomBetweenAnswer%${divisibleBy})) -ne 0 ] \ |
| 177 | && echo DIVISIBLE BY error - ${randomBetweenAnswer}! |
| 178 | |
| 179 | # Store the answer away statistically. |
| 180 | answer[randomBetweenAnswer+disp]=$((answer[randomBetweenAnswer+disp]+1)) |
| 181 | done |
| 182 | |
| 183 | |
| 184 | |
| 185 | # Let's check the results |
| 186 | |
| 187 | for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do |
| 188 | [ ${answer[i+disp]} -eq 0 ] \ |
| 189 | && echo "We never got an answer of $i." \ |
| 190 | || echo "${i} occurred ${answer[i+disp]} times." |
| 191 | done |
| 192 | |
| 193 | |
| 194 | exit 0 |