coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generating pseudo-random integers


From: Eric Blake
Subject: Re: Generating pseudo-random integers
Date: Fri, 04 Feb 2011 21:35:43 -0700
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.9) Gecko/20100827 Red Hat/3.1.3-1.el6 Mnenhy/0.8.3 Thunderbird/3.1.3

On 02/04/2011 08:48 PM, Melikamp T. Medley wrote:
> Hi!
> 
> I want to generate pseudo-random integers from a shell, and I wonder if
> coreutils can be used to do that. I really like randint.c , and I
> basically want a shell front end to randint_choose. Or is there a
> standard way to do this I am not aware of?

If you don't mind using bash extensions, evaluating $RANDOM is the
fastest way to access a pseudo-random number from the shell.  Or,
several utilities provide random numbers, such as awk with its rand()
function, or perl.

Using just coreutils, though, I'm afraid your options are slim.  You
could use 'seq 0 9 | sort -R | head -n1' (or use shuf instead of sort
-R) to generate one random number at a time, but that gets expensive.
Or, if you don't mind limiting yourself to 62**6 values (instead of the
more traditional 2**32), you could do 'mkdir tmp; mktemp -u -p tmp
XXXXXX'.  But that's the extent of coreutils' randomness exposed to the
user.

-- 
Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]