Generate a pseudo-random 6-digit number

let N=0 N=N+10**{0..5}*${RANDOM:0:1}

December 7, 2019CK

Explanation

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.

Limitations

This method doesn't preclude the possibility of leading zeroes counting towards the 6-digit total.

Related one-liners

Generate a random number of up to 6 digits

python -c 'import random; print(random.randint(0, 10**6 - 1))'

September 19, 2018johntellsall