Re:Arc4random – randomization for all occasions
by acveilleux on Friday November 21, 2014 @02:02PM
(#8642710)
It's simpler to dd from /dev/[u]random however and in OpenBSD, you'll get exactly the same quality result. Most of the benefits of arc4random() are nullified by invoking gcc...
Re:Arc4random – randomization for all occasions
by tedunangst on Friday November 21, 2014 @03:40PM
(#8643265)
On OpenBSD, it's even simpler to just echo $RANDOM, but that only gets you 15 bits because sh. sysctl -n kern.arandom can be used in a pinch, too.
Re:Arc4random – randomization for all occasions
by eridius on Friday November 21, 2014 @05:40PM
(#8643808)
Theoretically couldn't you do something like
echo $(( $RANDOM << 15 | $RANDOM ))
to get 30 bits?
Re:Arc4random – randomization for all occasions
by 101914 on Friday November 21, 2014 @09:55PM
(#8644543)
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?
Re:Arc4random – randomization for all occasions
by acveilleux on Saturday November 22, 2014 @10:42AM
(#8645746)
This 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.
Re:Arc4random – randomization for all occasions
by 101914 on Saturday November 22, 2014 @01:46PM
(#8646269)
Agreed. 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
Re:Arc4random – randomization for all occasions
by Sanddancer on Saturday November 22, 2014 @02:48AM
(#8645085)
$RANDOM is shell-specific. bash has $RANDOM, ksh has $RANDOM, and zsh has $RANDOM, standard sh doesn't have it.
For example to choose a pseudorandom entry from a (reasonably short) list and set it as an environmental variable:
echo "main(){ arc4random();}" |gcc -x c -pipe -o arc4random -static -
b1=$(sed -n '$!D;=' .list); c1=$((b1/255)); arc4random; d1=$(($?*c1)); VARIABLE=$( sed ''"$d1"'!d;'"$d1"'q' .list ); echo $VARIABLE