5 ms·
Could someone explain how? I don't grok the command. My best guess is this relates to hexadecimal.
by wdutch 4y ago
Could someone explain how? I don't grok the command. My best guess is this relates to hexadecimal.
- Retr0id 4y ago`od -N 1 -An -i` reads one byte from stdin (urandom in this case) and prints it as a decimal string (with value between 0 and 255). The awk command receives that number and prints it back out modulo 10.
- wdutch 4y agoThanks for the explanation!
- cygx 4y agoThe calculation maps a random number R in range 0..255 to numbers 1..10 via R % 10 + 1 The problem is that 10 does not evenly divide 256, hence for large R, the mapping will be incomplete. Specifically, 250 => 1 251 => 2 252 => 3 253 => 4 254 => 5 255 => 6 with no more random numbers available to yield results 7, 8, 9 and 10.
- wdutch 4y agoThanks for the explanation!
- demindiro 4y agoTo add to this: (one of) the correct solution(s) is R * 10 / 256 + 1 EDIT: I am wrong, this still has bias.
- Spivak 4y agoYeah, an easy solution to all these things is “find a uniformly random source that outputs in a range larger but close to what you want and then try again if it’s outside your range. And indeed this is how Python does it https://github.com/python/cpython/blob/e37ac5fbb6de593521cf218aa05bc58a45c5a7c9/Lib/random.py#L238 https://github.com/python/cpython/blob/e37ac5fbb6de593521cf2... https://github.com/python/cpython/blob/e37ac5fbb6de593521cf218aa05bc58a45c5a7c9/Lib/random.py#L248 https://github.com/python/cpython/blob/e37ac5fbb6de593521cf2...