On Sat, Aug 29, 2015 at 12:26:37PM +0200, Matthieu Herrb wrote:
| On Fri, Aug 28, 2015 at 11:01:45AM +0200, Martijn van Duren wrote:
| > Here's some potential useful feature from my .profile. It might not be
| > perfect, but at least it's better then some of the generators I've seen in
| > production.
| >
| > # $RANDOM is not portable and in ksh it's limited to 32767.
| > rand() {
| >     local number
| >     local rdevice
| >
| >     for rdevice in /dev/{u,}random fail; do
| >             test -c $rdevice -a -r $rdevice && break
| >     done
| >     test $rdevice = "fail" && return 1
| >
| >     number=$(dd if=$rdevice bs=4 count=1 2>/dev/null | hexdump -e '/4 
"%u\n"')
| >     if [ -z "$1" ]; then
| >             echo $number
| >     else
| >             echo $(($number % $(($1 + 1))))
| >     fi
| > }
| 
| One can use 'openssl rand' here for results of the same quality in
| less lines.

For better quality (no modulo bias) random values, pkg_add ranval.
Uses OpenBSD's arc4random family of functions.  Because of this, it
doesn't open devices (easy to build into a static binary and use in
chroots).

Cheers,

Paul 'WEiRD' de Weerd

-- 
>++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
                 http://www.weirdnet.nl/                 

Reply via email to