Advertisement
pintcat

whrandom.sh - POSIX compliant random number generator using Wichmann-Hill method

Oct 29th, 2023 (edited)
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.85 KB | Science | 0 0
  1. #####################################################################################################################
  2. # whrandom v2.6 - POSIX compliant random number generator using old or new improved Wichmann-Hill method from 2006. #
  3. # Handshake:                                                                                                        #
  4. # $WH_MAX contains the maximum value for the random number. If set to 0 no limiter will be applied (default: 0).    #
  5. # $WH_LOOP contains the number of random numbers to be generated. Set it to "U" for an unlimited random number      #
  6. #  generation (default: 1).                                                                                         #
  7. # $WH_PREC contains the precision level for the calculation. The lower the level the less percise and thus less     #
  8. #  random the result will be. Must be between 1 and 9 (default: 9).                                                 #
  9. # $WH_DEL contains the delimiter used to separate multiple values (default: \n (new line)).                         #
  10. # $WH_ALGO tells which algorithm will be used - WH_OLD or WH_NEW (default: WH_NEW).                                 #
  11. # Usage:                                                                                                            #
  12. # Simply call WH_RANDOM() to generate one or more random numbers. These numbers will be written to stdout.          #
  13. # The old Wichmann-Hill algorrithm requires 3, the new one 4 seeds each between 1 and 30000. By default, whrandom   #
  14. # automatically creates 4 unique seeds using the millisecond counter of the system clock. Alternatively, you can    #
  15. # call WH_RANDOM() with up to 4 integers. They will be checked and exchanged with automatically generated ones if   #
  16. # they don't fit the needs. Beware: If you use 4 fixed seeds whrandom will always create an identical chain of      #
  17. # random numbers as long as you use the same seeds.                                                                 #
  18. #####################################################################################################################
  19.  
  20. WH_MAX=0
  21. WH_LOOP=1
  22. WH_PREC=9
  23. WH_DEL="\n"
  24. WH_ALGO=WH_NEW
  25.  
  26. # WH_CHECK() - checks if a value is given, if this value is a number and if it's between 1 and 30000. If so it
  27. #  returns that very number and if not it generates a suitable one using the system clock's millisecond counter.
  28. # IN:
  29. # $1 should contain an integer between 1 and 30000.
  30. # OUT:
  31. # Prints the fitting number to stdout.
  32.  
  33. WH_CHECK(){
  34.     if [ -n "${1##*[!0-9]*}" ] && [ $1 -gt 0 ] && [ $1 -le 30000 ]; then
  35.         printf $1
  36.     else
  37.         WH_TMP=$(date +%5N)
  38.         WH_TMP=$((${WH_TMP#${WH_TMP%%[1-9]*}}+1))    # remove leading zeros & add 1 to prevent 0 as value
  39.         [ $WH_TMP -gt 30000 ] && WH_TMP=$((WH_TMP/4))
  40.         printf $WH_TMP
  41.     fi
  42. }
  43.  
  44. # WH_OLD() - generates a random number using the older 16bit integer based algorithm.
  45. # IN:
  46. # $SEED1, $SEED2 & $SEED3 contain the values needed by the algorithm for computation.
  47. # $WH_F contains a decimal factor to produce larger, more precise random integers.
  48. # OUT:
  49. # $WH_RND contains the random number to be handed over to the core function for further operations.
  50.  
  51. WH_OLD(){
  52.     WH_SEED1=$(((171*$WH_SEED1)%30269))
  53.     WH_SEED2=$(((172*$WH_SEED2)%30307))
  54.     WH_SEED3=$(((170*$WH_SEED3)%30323))
  55.     WH_RND=$((($WH_SEED1*$WH_F/30269)+($WH_SEED2*$WH_F/30307)+($WH_SEED3*$WH_F/30323)))
  56. }
  57.  
  58. # WH_NEW() - generates a random number using the newer 32bit integer based algorithm.
  59. # IN:
  60. # $SEED1, $SEED2, $SEED3 & $SEED4 contain the values needed by the algorithm for computation.
  61. # $WH_F contains a decimal factor to produce larger, more precise random integers.
  62. # OUT:
  63. # $WH_RND contains the random number to be handed over to the core function for further operations.
  64.  
  65. WH_NEW(){
  66.     [ $((WH_SEED1=11600*($WH_SEED1%185127)-10379*($WH_SEED1/185127))) -lt 0 ] && WH_SEED1=$(($WH_SEED1+2147483579))
  67.     [ $((WH_SEED2=47003*($WH_SEED2%45688)-10479*($WH_SEED2/45688))) -lt 0 ] && WH_SEED2=$(($WH_SEED2+2147483543))
  68.     [ $((WH_SEED3=23000*($WH_SEED3%93368)-19423*($WH_SEED3/93368))) -lt 0 ] && WH_SEED3=$(($WH_SEED3+2147483423))
  69.     [ $((WH_SEED4=33000*($WH_SEED4%65075)-8123*($WH_SEED4/65075))) -lt 0 ] && WH_SEED4=$(($WH_SEED4+2147483123))
  70.     WH_RND=$((($WH_SEED1*$WH_F/2147483579)+($WH_SEED2*$WH_F/2147483543)+($WH_SEED3*$WH_F/2147483423)+($WH_SEED4*$WH_F/2147483123)))
  71. }
  72.  
  73. # WH_RANDOM() - core function: prepares seeds and generates the output.
  74. # IN:
  75. # $1, $2, $3 and $4 can contain integers between 1 and 30000 to be used as seeds. These values will be checked and -
  76. #  if not suitable - replaced with auto generated ones.
  77. # $WH_PREC is the precision level. Each seed is multiplied with 10^$WH_PREC to receive a larger, more accurate value.
  78. # $WH_LOOP tells how many numbers will be generated. "U" will generate a never ending chain of random numbers.
  79. # $WH_RND contains the random number which was handed over by the algorithm for further operations.
  80. # $WH_MAX contains the maximum value which will not be exceeded by the random number. 0 leaves the result unaltered.
  81. # $WH_DEL contains the delimiter which is placed after each value.
  82. # OUT:
  83. # $WH_F contains a decimal factor used by the algorithm to produce larger, more precise random integers.
  84. # $WH_RND contains the current random number
  85. # Also prints the random numbers with delimiter appended to stdout.
  86.  
  87. WH_RANDOM(){
  88.     WH_F=10
  89.     WH_TMP=$WH_PREC
  90.     while [ $((WH_TMP=$WH_TMP-1)) -gt 0 ]; do
  91.         [ $WH_F -lt 1000000000 ] && WH_F=$(($WH_F*10)) || WH_TMP=1
  92.     done
  93.     WH_SEED1=$(WH_CHECK $1)
  94.     WH_SEED2=$(WH_CHECK $2)
  95.     WH_SEED3=$(WH_CHECK $3)
  96.     WH_SEED4=$(WH_CHECK $4)
  97.     while [ $WH_LOOP = U ] || [ $((WH_TMP=$WH_TMP+1)) -le $WH_LOOP ]; do
  98.         $WH_ALGO
  99.         if [ $WH_MAX -gt 0 ]; then
  100.             WH_RND=$(($WH_RND%$WH_F*$WH_MAX))
  101.             printf "%.0f$WH_DEL" "${WH_RND}e-$WH_PREC"    # crop down to desired length & round remaining integer
  102.         else
  103.             printf $WH_RND"$WH_DEL"
  104.         fi
  105.     done
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement