Advertisement
pintcat

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

Oct 29th, 2023 (edited)
1,439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.22 KB | Science | 0 0
  1. ######################################################################################################################
  2. # whrandom v2.14 - 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:  $1 should contain an integer between 1 and 30000.
  29. # OUT: Prints the fitting number to stdout.
  30.  
  31. WH_CHECK(){
  32.     if [ -n "${1##*[!0-9]*}" ] && [ $1 -gt 0 ] && [ $1 -le 30000 ]; then
  33.         printf $1
  34.     else
  35.         WH_TMP=$(date +%5N)
  36.         WH_TMP=$((${WH_TMP#${WH_TMP%%[1-9]*}}+1))    # remove leading zeros & add 1 to prevent 0 as value
  37.         [ $WH_TMP -gt 30000 ] && WH_TMP=$((WH_TMP/4))
  38.         printf $WH_TMP
  39.     fi
  40. }
  41.  
  42. # WH_OLD() - generates a random number using the older 16bit integer based algorithm.
  43. # IN:  $WH_S1, $WH_S2 & $WH_S3 contain the values (seeds) needed by the algorithm for computation.
  44. #      $WH_F contains a decimal factor to produce larger, more precise random integers.
  45. # OUT: $WH_RND contains the random number to be handed over to the core function for further operations.
  46.  
  47. WH_OLD(){
  48.     [ $((WH_S1=171*($WH_S1%177)-2*($WH_S1/177))) -lt 0 ] && WH_S1=$(($WH_S1+30269))
  49.     [ $((WH_S2=172*($WH_S2%176)-35*($WH_S2/176))) -lt 0 ] && WH_S2=$(($WH_S2+30307))
  50.     [ $((WH_S3=170*($WH_S3%178)-63*($WH_S3/178))) -lt 0 ] && WH_S3=$(($WH_S3+30323))
  51.     WH_RND=$((($WH_S1*$WH_F/30269)+($WH_S2*$WH_F/30307)+($WH_S3*$WH_F/30323)))
  52. }
  53.  
  54. # WH_NEW() - generates a random number using the newer 32bit integer based algorithm.
  55. # IN:  $WH_S1, $WH_S2, $WH_S3 & $WH_S4 contain the values (seeds) needed by the algorithm for computation.
  56. #      $WH_F contains a decimal factor to produce larger, more precise random integers.
  57. # OUT: $WH_RND contains the random number to be handed over to the core function for further operations.
  58.  
  59. WH_NEW(){
  60.     [ $((WH_S1=11600*($WH_S1%185127)-10379*($WH_S1/185127))) -lt 0 ] && WH_S1=$(($WH_S1+2147483579))
  61.     [ $((WH_S2=47003*($WH_S2%45688)-10479*($WH_S2/45688))) -lt 0 ] && WH_S2=$(($WH_S2+2147483543))
  62.     [ $((WH_S3=23000*($WH_S3%93368)-19423*($WH_S3/93368))) -lt 0 ] && WH_S3=$(($WH_S3+2147483423))
  63.     [ $((WH_S4=33000*($WH_S4%65075)-8123*($WH_S4/65075))) -lt 0 ] && WH_S4=$(($WH_S4+2147483123))
  64.     WH_RND=$((($WH_S1*$WH_F/2147483579)+($WH_S2*$WH_F/2147483543)+($WH_S3*$WH_F/2147483423)+($WH_S4*$WH_F/2147483123)))
  65. }
  66.  
  67. # WH_RANDOM() - core function: prepares seeds and generates the output.
  68. # IN:  $1, $2, $3 and $4 can contain integers between 1 and 30000 to be used as seeds. These values will be checked and -
  69. #       if not suitable - replaced with auto generated ones.
  70. #      $WH_PREC is the precision level. Each seed is multiplied with 10^$WH_PREC to receive a larger, more accurate value.
  71. #      $WH_LOOP tells how many numbers will be generated. "U" will generate a never ending chain of random numbers.
  72. #      $WH_RND contains the random number which was handed over by the algorithm for further operations.
  73. #      $WH_MAX contains the maximum value which will not be exceeded by the random number. 0 leaves the result unaltered.
  74. #      $WH_DEL contains the delimiter which is placed after each value.
  75. # OUT: $WH_F contains a decimal factor used by the algorithm to produce larger, more precise random integers.
  76. #      $WH_SEEDS contains the original seeds used for the 1st iteration.
  77. #      $WH_RND contains the current random number
  78. #      Also prints the random numbers with delimiter appended to stdout.
  79.  
  80. WH_RANDOM(){
  81.     WH_F=10
  82.     WH_TMP=$WH_PREC
  83.     while [ $((WH_TMP=$WH_TMP-1)) -gt 0 ]; do
  84.         [ $WH_F -lt 1000000000 ] && WH_F=$(($WH_F*10)) || WH_TMP=1
  85.     done
  86.     WH_S1=$(WH_CHECK $1)
  87.     WH_S2=$(WH_CHECK $2)
  88.     WH_S3=$(WH_CHECK $3)
  89.     WH_S4=$(WH_CHECK $4)
  90.     WH_SEEDS="$WH_S1 $WH_S2 $WH_S3 $WH_S4"
  91.     while [ $WH_LOOP = U ] || [ $((WH_TMP=$WH_TMP+1)) -le $WH_LOOP ]; do
  92.         $WH_ALGO
  93.         [ $WH_MAX -gt 0 ] && printf $(((($WH_RND%$WH_F*$WH_MAX)+$WH_F*10/18)/$WH_F))"$WH_DEL" || printf $WH_RND"$WH_DEL"
  94.         # former rounding routine using parameter expansion - faster, but less accurate & less secure (broken in latest bash)
  95.         #WH_RND=$(($WH_RND%$WH_F*$WH_MAX))
  96.         #[ $WH_MAX -gt 0 ] && printf "%.0f$WH_DEL" "${WH_RND}e-$WH_PREC" || printf $WH_RND"$WH_DEL"
  97.     done
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement