Bash RANDOM with seed?
Assign a seed value to RANDOM
$ bash -c 'RANDOM=640; echo $RANDOM $RANDOM $RANDOM'
28612 27230 24923
$ bash -c 'RANDOM=640; echo $RANDOM $RANDOM $RANDOM'
28612 27230 24923
$ bash -c 'RANDOM=640; echo $RANDOM $RANDOM $RANDOM'
28612 27230 24923
$
Notice that single quotes are used; double quotes run afoul shell interpolation rules:
$ bash -c 'RANDOM=42; echo $RANDOM $RANDOM $RANDOM'
19081 17033 15269
$ RANDOM=42
$ bash -c "RANDOM=640; echo $RANDOM"
19081
$ bash -c "RANDOM=640; echo $RANDOM"
17033
$ bash -c "RANDOM=640; echo $RANDOM"
15269
$
because $RANDOM
is being interpolated by the parent shell before the child bash -c ...
process is run. Either use single quotes to turn off interpolation (as shown above) or otherwise prevent the interpolation:
$ RANDOM=42
$ SEED_FOR_MY_GAME=640
$ bash -c "RANDOM=$SEED_FOR_MY_GAME; echo \$RANDOM"
28612
$ bash -c "RANDOM=$SEED_FOR_MY_GAME; echo \$RANDOM"
28612
$
This feature of RANDOM
is mentioned in the bash(1)
manual
RANDOM Each time this parameter is referenced, a random integer between
0 and 32767 is generated. The sequence of random numbers may be
initialized by assigning a value to RANDOM. If RANDOM is unset,
it loses its special properties, even if it is subsequently
reset.