3 ms·
Why? Wiki doesn't say anything about it and when I run it, the result looks fine > $ for((i=1;i<10000;i+=1)); do cat /dev/urandom | od -N 1 -An -i | awk '{prin
by yonixw 4y ago
Why? Wiki doesn't say anything about it and when I run it, the result looks fine
> $ for((i=1;i<10000;i+=1)); do cat /dev/urandom | od -N 1 -An -i | awk '{print $1 % 10 + 1}' >> rand.txt ; done
> $ cat rand.txt | sort | uniq -c
1051 1
932 10
1012 2
1042 3
983 4
1015 5
1042 6
1035 7
992 8
995 9
- 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?
- Retr0id 4y agoIf you sum the occurrences of numbers 1-6, you get 1051 + 1012 + 1042 + 983 + 1015 + 1042 = 6145. The expected count (if the numbers were uniformly random) is only 6000, on average. If you repeat your experiment, you will find that this sum is greater than 6000 more often than not.