3 ms·
Whenever I'm asked for a 4 digit PIN: `echo $[RANDOM%10000]`
by linsomniac 2y ago
Whenever I'm asked for a 4 digit PIN: `echo $[RANDOM%10000]`
- aidenn0 2y agoYou should probably use SRANDOM; less bias from the modulo (32 bits instead of 15) and uses arc4random or /dev/urandom if available.
- linsomniac 2y agoTIL, thanks!
- devnullbrain 2y agoI'll remember to prioritise 0000 to 2767 for your PINs, then.
- shmuppet 2y agoJust so you can see the bias to early numbers in the distribution: for i in $(seq 1000000); do echo $[RANDOM%10000]; done | sort -n | uniq -c | sort -rn | gnuplot -e "set terminal dumb; set xtics 1000; plot '< cat' using 0:1 with boxes"
- aidenn0 2y agoI think this gnuplot command makes the bias much more obvious (and even better with -persist and "set terminal x11"): gnuplot -e "set terminal dumb; set xtics 100; plot '<cat'" Compare to the version that discards values over 3e4: for i in $(seq 1000000); do x=$((RANDOM)); while test $x -gt 30000;do x=$((RANDOM)); done; echo $((x%10000)); done |sort|uniq -c |sort -rn |gnuplot -e "set terminal dumb; set xtics 100; plot '<cat'" Or the version that uses the 32-bit SRANDOM, which reduces the bias by a factor of 2**17: for i in $(seq 1000000); do echo $((SRANDOM%10000)); done | sort -n | uniq -c | sort -rn |gnuplot -e "set terminal dumb; set xtics 100; plot '<cat'"