Advertisement
A_GUES

Bash hack RNG

Jun 22nd, 2023
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.16 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. sequence=(
  4.     0.9311600617849973
  5.     0.3551442693830502
  6.     0.7923158995678377
  7.     0.787777942408997
  8.     0.376372264303491
  9. )
  10.  
  11. sequence=($(echo "${sequence[@]}" | tac))
  12.  
  13. for ((i=${#sequence[@]}-1; i>=0; i--)); do
  14.     se_s1=$se_state0
  15.     se_s0=$se_state1
  16.     se_state0=$se_s0
  17.     se_s1=$((se_s1 ^ (se_s1 << 23)))
  18.     se_s1=$((se_s1 ^ (se_s1 >> 17)))
  19.     se_s1=$((se_s1 ^ se_s0))
  20.     se_s1=$((se_s1 ^ (se_s0 >> 26)))
  21.     se_state1=$se_s1
  22.  
  23.     float_64=$(echo "${sequence[i]} + 1" | awk '{print $1}')
  24.     u_long_long_64=$(python -c "import struct; print(struct.unpack('<Q', struct.pack('d', $float_64))[0])")
  25.  
  26.     mantissa=$((u_long_long_64 & ((1 << 52) - 1)))
  27.  
  28.     if [ $mantissa -eq $((se_state0 >> 12)) ]; then
  29.         echo "Mantissa match found at index $i"
  30.         state0=$se_state0
  31.  
  32.         u_long_long_64=$((state0 >> 12 | 0x3FF0000000000000))
  33.         float_64=$(python -c "import struct; print(struct.unpack('d', struct.pack('<Q', $u_long_long_64))[0])")
  34.         next_sequence=$(echo "$float_64 - 1" | awk '{print $1}')
  35.  
  36.         echo "Next sequence: $next_sequence"
  37.         exit 0
  38.     fi
  39. done
  40.  
  41. echo "No match found"
  42. exit 1
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement