3 ms·
`cat /dev/urandom | od -N 1 -An -i` gives you a uniform random number between 0 and 255. The following mod 10 in the awk command introduces "modulo bias", which
by Retr0id 4y ago
`cat /dev/urandom | od -N 1 -An -i` gives you a uniform random number between 0 and 255. The following mod 10 in the awk command introduces "modulo bias", which you can read about here https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/ https://research.kudelskisecurity.com/2020/07/28/the-definit...
You can see it quite intuitively if you iterate over all possible numbers between 0 and 255:
$ (for i in {0..255}; do; echo $i | awk '{print $1 % 10 + 1}'; done) | sort -n | uniq -c
26 1
26 2
26 3
26 4
26 5
26 6
25 7
25 8
25 9
25 10
- yonixw 4y agoBut that is not how /dev/(u)random works and does not give U(255). Generally speaking, it gives some hash of some user/device activity, from these resources: https://linuxhint.com/dev_random_vs_dev_urandom/ https://linuxhint.com/dev_random_vs_dev_urandom/ https://en.wikipedia.org/wiki//dev/random https://en.wikipedia.org/wiki//dev/random
- Retr0id 4y agoI know how urandom works (consider reading the articles you linked). The number between 0 and 255 is produced by the `od` command: https://man7.org/linux/man-pages/man1/od.1.html https://man7.org/linux/man-pages/man1/od.1.html
- deleted 4y ago[deleted]
- messe 4y ago> does not give U(255) What do you think each byte from /dev/(u)random is?
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- moyix 4y agoJust to be clear, cryptographic hashes and other outputs of cryptographic primitives are designed to be uniform random. If you find a detectable bias from uniform on the outputs of /dev/urandom then you should consider the underlying primitive (the ChaCha20 stream cipher, in the case of modern Linux urandom) to be broken.
- krick 4y agoSo, is there a correct way to do it in bash?