4 ms·
OK, I feel stupid. How would I get a random number using dd and /dev/urandom? I am not sure I understand the gcc comment. Can you explain? Assuming I cannot
by 101914 12y ago
OK, I feel stupid. How would I get a random number using dd and /dev/urandom?
I am not sure I understand the gcc comment. Can you explain?
Assuming I cannot get a random number from dd'ing /dev/urandom then I needed a utility to do it where the system only has a small base and does not have gcc.
Unangst's suggestion to use sysctl is better (although it means I have to crunch the sysctl utility). I will be using that from now on. Thank you for the tip!
As for $RANDOM, that may be OpenBSD-specific. For example, does NetBSD have it?
- Sanddancer 12y ago$RANDOM is shell-specific. bash has $RANDOM, ksh has $RANDOM, and zsh has $RANDOM, standard sh doesn't have it.
- 101914 12y agoAh, I had forgotten where this comes from. This makes sense; OpenBSD's default shell is pdksh or some derivative thereof, if I recall correctly.
- acveilleux 12y agoThis dd(1) command line would get 1 block of 4 bytes from the /dev/random file and output it to stdout. Effectively the same as a call to arc4random(): dd if=/dev/random count=1 bs=4 On Linux, you'd use /dev/urandom cause /dev/random blocks stupidly.
- 101914 12y agoAgreed. But I want a pseudorandom real _number_ in decimal, to use in a shell script, not just random bytes. I gave your solution a try. a=$(dd if=/dev/urandom bs=4 count=1|od -An -tx1|sed 's/ //g') printf %d\\n 0x$a