help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How random is $RANDOM?


From: Andreas Kusalananda Kähäri
Subject: Re: [Help-bash] How random is $RANDOM?
Date: Thu, 9 May 2019 14:46:59 +0200
User-agent: Mutt/1.11.4 (2019-03-13)

Re-sent since I managed to send it from the wrong address the first time
around.

On Thu, May 09, 2019 at 07:29:51AM -0500, Peng Yu wrote:
> Why only 16 bits $RANDOM is supported? Is there a problem to set 32
> bits as the default?

If you are writing an application that has very specific requirements
with regards to random number generation, it strikes me that writing it
as a shell script may not be optimal.  You may want to use a generator
ased on e.g. arc4random() [1], or to use that function directly from a
C program.


[1] https://man.openbsd.org/arc4random

Regards,
Kusalananda

> 
> On 5/9/19, Andreas Kusalananda Kähäri <address@hidden> wrote:
> > On Thu, May 09, 2019 at 06:33:07AM -0500, Peng Yu wrote:
> >> Let's say two bash processes call $RANDOM at the exact same time. Will
> >> the results be the same? Or the results will be different.
> >
> > The random number generator used in bash is seeded using a number that
> > contains the PID of the current shell (as well as the current time).
> >
> > https://git.savannah.gnu.org/cgit/bash.git/tree/variables.c#n1369
> >
> >
> >>
> >> Also, is the low bits of $RANDOM more random or the high bits of
> >> $RANDOM more random. I see examples using $RANDOM%n to map the random
> >> number to a smaller range. But my understanding in other random
> >> numbers is that the high bits are more random and using the modulus is
> >> discouraged. I am not sure what is the recommended usage of $RANDOM to
> >> map it to a smaller range. Does anybody know the best use of $RANDOM
> >> in this case? Thanks.
> >
> > The random number generator itself is short and has a reference to the
> > relevant paper describing it.
> >
> > https://git.savannah.gnu.org/cgit/bash.git/tree/variables.c#n1329

-- 
Kusalananda
Sweden



reply via email to

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