Advertisement
rockdrilla

random() in pure sh

Jul 17th, 2017
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.26 KB | None | 0 0
  1. random() {
  2.   local n x
  3.   if [ $1 -le 0 ]; then
  4.     echo 0
  5.     return
  6.   fi
  7.   if [ $1 -lt 255 ]; then
  8.     n=1
  9.   elif [ $1 -lt 65535 ]; then
  10.     n=2
  11.   else
  12.     n=4
  13.   fi
  14.   x='0x'$(dd if=/dev/urandom bs=$n count=1 2>/dev/null | xxd -ps)
  15.   echo $((x % $1))
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement