let N=0 N=N+10**{0..5}*${RANDOM:0:1}
Rather than running Python to generate a random number, we can use builtin features of Bash to do this more quickly and with less code.
RANDOM
is a Bash variable that expands to a new random number in the range 0 to 32767 every time it is referenced.
{0..5}
expands to the numbers 0 1 2 3 4 5.
${RANDOM:0:1}
is the first digit of a random number.
let
enables an arithmetic context in Bash. We initialize N
to 0, generate 6 random digits multiplied by increasing powers of 10, effectively building a 6-digit random number:
๐ต = ๐โ + 10๐โ + 100๐โ + 1000๐โ + 10000๐โ
+ 100000๐โ
where ๐แตข
are the 6 initial digits taken from the 6 calls to $RANDOM
.
This method doesn't preclude the possibility of leading zeroes counting towards the 6-digit total.
python -c 'import random; print(random.randint(0, 10**6 - 1))'