Python 3.9 random.randbytes
+ random.seed
I've learnt to stop fighting what Bash can't do and just go with the flow:
randbytes() (
python -c 'import random;import sys;random.seed(int(sys.argv[1]));sys.stdout.buffer.write(random.randbytes(int(sys.argv[2])))' "$@"
)
Usage:
randbytes <seed> <nbytes>
e.g.:
randbytes 0 8 | hd
always outputs 8 identical pseudo-random bytes with seed 0:
00000000 cd 07 2c d8 be 6f 9f 62 |..,..o.b|
00000008
Readable multiline version at: Generating random string of seedable data
On my Lenovo ThinkPad P51, I can dump 100 million bytes in ramfs in 0.5s. however, if I try to dump 1 billion it blows up with:
Python int too large to convert to C int
so it is something to keep in mind.
For comparison:
time sudo dd if=/dev/urandom of=ramfs/test bs=4k count=24414
took 2.5s, so it is slower, which is not surprising as it is a more random source, while the Python generator is deterministic and appears to be written in C.
Tested on Ubuntu 20.10, Linux kernel 5.8.0.