SHARE
TWEET

random-gen.sh

h8rt3rmin8r Aug 25th, 2019 (edited) 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #------------------------------------------------------------------------------#
  3. # [ random-gen.sh ]
  4. #
  5. # Generate pseudo-random numbers in Bash
  6. #
  7. # Adapted from from http://tldp.org/LDP/abs/html/randomvar.html
  8. # on 20190825 by h8rt3rmin8r.
  9. #
  10. # NOTES:
  11. #
  12. #   $RANDOM returns a different random integer at each invocation.
  13. #   Nominal range: 0 - 32767 (signed 16-bit integer).
  14. #
  15. #------------------------------------------------------------------------------#
  16.  
  17. LN_X="-----------------"
  18.  
  19. MAXCOUNT=10
  20. count=1
  21. echo
  22. echo "$MAXCOUNT random numbers:"
  23. echo "${LN_X}"
  24.  
  25. # Generate 10 ($MAXCOUNT) random integers.
  26. while [ "$count" -le $MAXCOUNT ];
  27. do
  28.     number=$RANDOM
  29.     echo $number
  30.     let "count += 1"  # Increment count.
  31. done
  32. echo "${LN_X}"
  33.  
  34. # If you need a random int within a certain range, use the 'modulo' operator.
  35. # This returns the remainder of a division operation.
  36. RANGE=500
  37. echo
  38. number=$RANDOM
  39. let "number %= $RANGE"
  40. #           ^^
  41. echo "Random number less than $RANGE  ---  $number"
  42. echo
  43.  
  44. #  If you need a random integer greater than a lower bound,
  45. #+ then set up a test to discard all numbers below that.
  46.  
  47. FLOOR=200
  48. number=0   #initialize
  49. while [ "$number" -le $FLOOR ];
  50.     do number=$RANDOM
  51. done
  52. echo "Random number greater than $FLOOR ---  $number"
  53. echo
  54.  
  55. # Let's examine a simple alternative to the above loop, namely
  56. #       let "number = $RANDOM + $FLOOR"
  57. # That would eliminate the while-loop and run faster.
  58. # But, there might be a problem with that. What is it?
  59.  
  60. # Combine above two techniques to retrieve random number between two limits.
  61. number=0   #initialize
  62. while [ "$number" -le $FLOOR ];
  63. do
  64.     number=$RANDOM
  65.     let "number %= $RANGE"  # Scales $number down within $RANGE.
  66. done
  67. echo "Random number between $FLOOR and $RANGE ---  $number"
  68. echo
  69.  
  70. # Generate binary choice, that is, "true" or "false" value.
  71. BINARY=2
  72. T=1
  73. number=$RANDOM
  74. let "number %= $BINARY"
  75.  
  76. #  Note that    let "number >>= 14"    gives a better random distribution
  77. #+ (right shifts out everything except last binary digit).
  78. if [ "$number" -eq $T ];
  79.     then echo "TRUE"
  80.     else echo "FALSE"
  81. fi  
  82. echo
  83.  
  84. # Generate a toss of the dice.
  85. # Modulo 6 gives range 0 - 5.
  86. # Incrementing by 1 gives desired range of 1 - 6.
  87. # (Thanks, Paulo Marcel Coelho Aragao, for the simplification.)
  88. SPOTS=6
  89. die1=0
  90. die2=0
  91. # Would it be better to just set SPOTS=7 and not add 1? Why or why not?
  92. # Tosses each die separately, and so gives correct odds.
  93. let "die1 = $RANDOM % $SPOTS +1" # Roll first one.
  94. let "die2 = $RANDOM % $SPOTS +1" # Roll second one.
  95.  
  96. #  Which arithmetic operation, above, has greater precedence --
  97. #+ modulo (%) or addition (+)?
  98.  
  99. let "throw = $die1 + $die2"
  100. echo "Throw of the dice = $throw"
  101. echo
  102. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top