randint
This commit is contained in:
parent
e0015b5dc7
commit
25194c1e55
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
My collection of useful shell scripts.
|
My collection of useful shell scripts.
|
||||||
|
|
||||||
|
### randint.sh
|
||||||
|
usage: randint <positive integer>
|
||||||
|
print in the terminal a random integer x with $` 0 \leq x \lt \$1`$
|
||||||
|
|
||||||
### killwine.sh
|
### killwine.sh
|
||||||
kill every wine process including processes hanging
|
kill every wine process including processes hanging
|
||||||
|
|
||||||
|
12
randint.sh
Executable file
12
randint.sh
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
|
||||||
|
echo "Usage: randint <positive integer>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
number=$1
|
||||||
|
ceiling=$(echo "l($number +1)/l(2)" | bc -l | awk '{
|
||||||
|
bits = ($1 == int($1)) ? $1 : int($1) + 1;
|
||||||
|
print int( (bits + 7) / 8 )
|
||||||
|
}')
|
||||||
|
random_num=$(xxd -p -l $ceiling /dev/urandom | tr -d '\n' | awk -v num="$number" '{print strtonum("0x" $0) % num}')
|
||||||
|
echo $random_num
|
Loading…
x
Reference in New Issue
Block a user