pintcat

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

Oct 29th, 2023 (edited)
1,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.55 KB | Science | 0 0
  1. ##################################################################################################################################
  2. # whrandom v2.31 - POSIX compliant random number generator using old or new improved Wichmann-Hill method.                       #
  3. # Handshake:                                                                                                                     #
  4. # $WH_MIN contains the minimum value for the random number (default: 0).                                                         #
  5. # $WH_MAX contains the maximum value for the random number. The limit is 4 billion. If rejection sampling is used the process    #
  6. #  will slow down significantly if the max value exceeds 999999999 (default: 4000000000).                                        #
  7. # $WH_LOOP tells how many random numbers to be generated. Set it to "U" for an unlimited random number generation (default: 1)   #
  8. # $WH_DEL contains the delimiter used to separate multiple values (default: \n (new line)).                                      #
  9. # $WH_ALGO tells which algorithm will be used - WH_OLD, WH_NEW or WH_NEW64() (default: WH_NEW).                                  #
  10. # $WH_SCALE sets the scaling mode if $WH_MIN and/or $WH_MAX is used. Leave empty to trigger the slower, but better rejection     #
  11. #  sampling method. Set it to anything to trigger a simpler, but slightly faster rounding method (default: fast).                #
  12. # Usage:                                                                                                                         #
  13. # Simply call WH_RANDOM() to generate one or more random numbers. These numbers will be written to stdout.                       #
  14. # The old Wichmann-Hill algorrithm requires 3, the new one 4 seeds each between 1 and 30000. By default, whrandom automatically  #
  15. # creates 4 unique seeds using the millisecond counter of the system clock. Alternatively, you can call WH_RANDOM() with up to 4 #
  16. # integers. They will be checked and exchanged with automatically generated ones if they don't fit the needs. Note that if you   #
  17. # use 4 fixed seeds whrandom will always create an identical chain of random numbers as long as you use the same seeds.          #
  18. ##################################################################################################################################
  19.  
  20. WH_MIN=0
  21. WH_MAX=4000000000
  22. WH_LOOP=1
  23. WH_DEL="\n"
  24. WH_ALGO=WH_NEW
  25. WH_SCALE=fast
  26.  
  27. 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 returns that very
  28. #             number and if not it generates a suitable one using the system clock's nanosecond counter.
  29. # IN:  $1 should contain an integer between 1 and 30000.
  30. # OUT: Prints the fitting number to stdout.
  31.     if [ -n "${1##*[!0-9]*}" ] && [ $1 -gt 0 ] && [ $1 -le 30000 ]; then
  32.         printf $1
  33.     else # fetch 5 digits of nanoseconds; removes leading zeros & adds 1 to prevent zero; if value is >30000 divide by 4
  34.         WH_TMP=$(date +%5N)
  35.         [ $((WH_TMP=${WH_TMP#${WH_TMP%%[1-9]*}}+1)) -gt 30000 ] && WH_TMP=$(($WH_TMP/4))
  36.         printf $WH_TMP
  37.     fi
  38. }
  39.  
  40. WH_OLD(){ # generates a random number using the older 16bit integer based algorithm.
  41. # IN:  $WH_S1, $WH_S2 & $WH_S3 contain the values (seeds) needed by the algorithm for computation.
  42. # OUT: $WH_RND contains the random number to be handed over to the core function for further operations.
  43.     [ $((WH_S1=171*($WH_S1%177)-2*($WH_S1/177))) -lt 0 ] && WH_S1=$(($WH_S1+30269))
  44.     [ $((WH_S2=172*($WH_S2%176)-35*($WH_S2/176))) -lt 0 ] && WH_S2=$(($WH_S2+30307))
  45.     [ $((WH_S3=170*($WH_S3%178)-63*($WH_S3/178))) -lt 0 ] && WH_S3=$(($WH_S3+30323))
  46.     WH_RND=$(($WH_S1*1000000000/30269+$WH_S2*1000000000/30307+$WH_S3*1000000000/30323))
  47. }
  48.  
  49. WH_NEW(){ # generates a random number using the newer 32bit integer based algorithm.
  50. # IN:  $WH_S1, $WH_S2, $WH_S3 & $WH_S4 contain the values (seeds) needed by the algorithm for computation.
  51. # OUT: $WH_RND contains the random number to be handed over to the core function for further operations.
  52.     [ $((WH_S1=11600*($WH_S1%185127)-10379*($WH_S1/185127))) -lt 0 ] && WH_S1=$(($WH_S1+2147483579))
  53.     [ $((WH_S2=47003*($WH_S2%45688)-10479*($WH_S2/45688))) -lt 0 ] && WH_S2=$(($WH_S2+2147483543))
  54.     [ $((WH_S3=23000*($WH_S3%93368)-19423*($WH_S3/93368))) -lt 0 ] && WH_S3=$(($WH_S3+2147483423))
  55.     [ $((WH_S4=33000*($WH_S4%65075)-8123*($WH_S4/65075))) -lt 0 ] && WH_S4=$(($WH_S4+2147483123))
  56.     WH_RND=$(($WH_S1*1000000000/2147483579+$WH_S2*1000000000/2147483543+$WH_S3*1000000000/2147483423+$WH_S4*1000000000/2147483123))
  57. }
  58.  
  59. WH_NEW64(){ # same result as WH_NEW(), but uses 64bit integer based algorithm with less steps & thus being slightly faster.
  60.     WH_S1=$((11600*$WH_S1%2147483579))
  61.     WH_S2=$((47003*$WH_S2%2147483543))
  62.     WH_S3=$((23000*$WH_S3%2147483423))
  63.     WH_S4=$((33000*$WH_S4%2147483123))
  64.     WH_RND=$(($WH_S1*1000000000/2147483579+$WH_S2*1000000000/2147483543+$WH_S3*1000000000/2147483423+$WH_S4*1000000000/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 - if not
  69. #       suitable - replaced with auto generated ones.
  70. #      $WH_LOOP tells how many numbers will be generated. "U" will generate a never ending chain of random numbers.
  71. #      $WH_RND contains the random number which was handed over by the algorithm for further operations.
  72. #      $WH_MIN contains the minimum value which will not be underrun by the random number.
  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. #      $WH_SCALE tells which method is used to scale the random number. If left empty, rejection sampling is used which is slower, but
  76. #       with better diffusion & no bias. If set to anything, it uses a slightly faster rounding method with a little worse diffusion.
  77. # OUT: $WH_SEEDS contains the original seeds used for the 1st iteration.
  78. #      $WH_RNG is the range from $WH_MIN to $WH_MAX.
  79. #      $WH_LMT is the largest multiple of $WH_RNG which is still < or = 1000000000.
  80. #      $WH_LO contains the lower block (everything below 1000000000 if $WH_MAX exceeds 999999999).
  81. #      $WH_HI contains the block index to decide which range above 999999999 is used in addition to $WH_LO.
  82. #      $WH_RND contains the current random number
  83. #      Also prints the random numbers with delimiter appended to stdout.
  84.     WH_TMP=1
  85.     [ -z $WH_MAX ] || [ $WH_MAX -le 0 ] || [ $WH_MAX -gt 4000000000 ] && WH_MAX=4000000000
  86.     [ $WH_MIN -ge $WH_MAX ] && return 1
  87.     set -- $(WH_CHECK "$1") $(WH_CHECK "$2") $(WH_CHECK "$3") $(WH_CHECK "$4")
  88.     WH_S1=$1 WH_S2=$2 WH_S3=$3 WH_S4=$4
  89.     WH_SEEDS="$WH_S1 $WH_S2 $WH_S3 $WH_S4"
  90.     while [ $WH_LOOP = U ] || [ $((WH_TMP=$WH_TMP+1)) -le $(($WH_LOOP+1)) ]; do
  91.         $WH_ALGO
  92.         if [ -z $WH_SCALE ]; then
  93.             if [ $((WH_RNG=$WH_MAX-$WH_MIN+1)) -gt 1000000000 ]; then
  94.                 WH_LO=$(($WH_RND%1000000000))
  95.                 $WH_ALGO
  96.                 WH_HI=$(($WH_RND%(($WH_RNG+999999999)/1000000000)))
  97.                 [ $((WH_RND=$WH_HI*1000000000+$WH_LO)) -lt $WH_RNG ] && printf $(($WH_RND+$WH_MIN))"$WH_DEL" || WH_TMP=$(($WH_TMP-1))
  98.             else
  99.                 WH_LMT=$((1000000000/$WH_RNG*$WH_RNG))
  100.                 [ $((WH_RND=$WH_RND%1000000000)) -lt $WH_LMT ] && printf $(($WH_RND%$WH_RNG+$WH_MIN))"$WH_DEL" || WH_TMP=$(($WH_TMP-1))
  101.             fi
  102.         elif [ -z $WH_MAX ] || [ $WH_MAX -ge 4000000000 ]; then
  103.             [ $WH_RND -ge $WH_MIN ] && printf $WH_RND"$WH_DEL" || WH_TMP=$(($WH_TMP-1))
  104.         else
  105.             printf $(((($WH_RND%1000000000*($WH_MAX-$WH_MIN)+555555555)/1000000000)+$WH_MIN))"$WH_DEL"
  106.         fi
  107.     done
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment