Advertisement
pintcat

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

Oct 29th, 2023 (edited)
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.91 KB | Science | 0 0
  1. ######################################################################################################################
  2. # whrandom v2.11 - 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:  $SEED1, $SEED2 & $SEED3 contain the values 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_SEED1=$(((171*$WH_SEED1)%30269))
  49.     WH_SEED2=$(((172*$WH_SEED2)%30307))
  50.     WH_SEED3=$(((170*$WH_SEED3)%30323))
  51.     WH_RND=$((($WH_SEED1*$WH_F/30269)+($WH_SEED2*$WH_F/30307)+($WH_SEED3*$WH_F/30323)))
  52. }
  53.  
  54. # WH_NEW() - generates a random number using the newer 32bit integer based algorithm.
  55. # IN:  $SEED1, $SEED2, $SEED3 & $SEED4 contain the values 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_SEED1=11600*($WH_SEED1%185127)-10379*($WH_SEED1/185127))) -lt 0 ] && WH_SEED1=$(($WH_SEED1+2147483579))
  61.     [ $((WH_SEED2=47003*($WH_SEED2%45688)-10479*($WH_SEED2/45688))) -lt 0 ] && WH_SEED2=$(($WH_SEED2+2147483543))
  62.     [ $((WH_SEED3=23000*($WH_SEED3%93368)-19423*($WH_SEED3/93368))) -lt 0 ] && WH_SEED3=$(($WH_SEED3+2147483423))
  63.     [ $((WH_SEED4=33000*($WH_SEED4%65075)-8123*($WH_SEED4/65075))) -lt 0 ] && WH_SEED4=$(($WH_SEED4+2147483123))
  64.     WH_RND=$((($WH_SEED1*$WH_F/2147483579)+($WH_SEED2*$WH_F/2147483543)+($WH_SEED3*$WH_F/2147483423)+($WH_SEED4*$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_RND contains the current random number
  77. #      Also prints the random numbers with delimiter appended to stdout.
  78.  
  79. WH_RANDOM(){
  80.     WH_F=10
  81.     WH_TMP=$WH_PREC
  82.     while [ $((WH_TMP=$WH_TMP-1)) -gt 0 ]; do
  83.         [ $WH_F -lt 1000000000 ] && WH_F=$(($WH_F*10)) || WH_TMP=1
  84.     done
  85.     WH_SEED1=$(WH_CHECK $1)
  86.     WH_SEED2=$(WH_CHECK $2)
  87.     WH_SEED3=$(WH_CHECK $3)
  88.     WH_SEED4=$(WH_CHECK $4)
  89.     while [ $WH_LOOP = U ] || [ $((WH_TMP=$WH_TMP+1)) -le $WH_LOOP ]; do
  90.         $WH_ALGO
  91.         [ $WH_MAX -gt 0 ] && printf $(((($WH_RND%$WH_F*$WH_MAX)+$WH_F*10/18)/$WH_F))"$WH_DEL" || printf $WH_RND"$WH_DEL"
  92.         #printf "%.0f$WH_DEL" "${WH_RND}e-$WH_PREC" # former rounding routine (stopped working with latest bash)
  93.     done
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement