Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

Difference between RANDOM and SRANDOM in Bash

Bash 5.1 introduces SRANDOM variable, but does it make any difference when used like this?

for i in {1..10}; do
  nbr=$((RANDOM%50))
  nbr1=$((SRANDOM%50))
  echo "$nbr -- $snbr"
done
9 -- 21
35 -- 43
27 -- 15
7 -- 24
41 -- 31
37 -- 35
23 -- 47
14 -- 23
9 -- 37
6 -- 30

From the manual:

RANDOM

Each time this parameter is referenced, it expands to a random integer between 0 and 32767. Assigning a value to this variable seeds the random number generator. If RANDOM is unset, it loses its special properties, even if it is subsequently reset

SRANDOM

This variable expands to a 32-bit pseudo-random number each time it is referenced. The random number generator is not linear on systems that support /dev/urandom or arc4random, so each returned number has no relationship to the numbers preceding it. The random number generator cannot be seeded, so assignments to this variable have no effect. If SRANDOM is unset, it loses its special properties, even if it is subsequently reset.

I don't understand what is meant by non-linear and seeding, but for my example is there a reason to use RANDOM over SRANDOM or vice-versa, or it doesn't make any difference?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

RANDOM (16-bit) vs SRANDOM (32-bit); 32-bit => much bigger random numbers (ie, more random numbers).

As for the randomness of the 2x variables ...

Try running:

RANDOM=5; for i in {1..10}; do echo $RANDOM; done
RANDOM=5; for i in {1..10}; do echo $RANDOM; done
RANDOM=5; for i in {1..10}; do echo $RANDOM; done

You should find that each loop generates the same set of 'random' numbers:

18499
9909
24640
15572
5516
17897
19000
12793
27730
5509

Here's a bash fiddle of the above that generates the same exact output!

Another test:

RANDOM=5; echo $RANDOM $RANDOM
RANDOM=5; echo $RANDOM $RANDOM
RANDOM=5; echo $RANDOM $RANDOM

Repeatedly generates:

18499 9909

Now repeat this test but with SRANDOM and you should find that you cannot seed SRANDOM and therefore each iteration generates a different set of 'random' numbers.

SRANDOM=5; echo $SRANDOM $SRANDOM
SRANDOM=5; echo $SRANDOM $SRANDOM
SRANDOM=5; echo $SRANDOM $SRANDOM

Which generates 3 different sets of output:

1355214790 3304840972
4217276959 255499591
446827805 3301635911

NOTE: Thanks to oguz ismail for the SRANDOM output.


As for the linear comment ...

As President James K Polk mentioned in his comment, 'linear' probably refers to the underlying algorithm by which RANDOM generates its not-so-random data.

Seeing how the RANDOM output can be determined beforehand (given a starting point, aka seed), I'd probably replace 'linear' with something more along the lines of 'deterministic' ...


Net result ... use RANDOM if you need to repeat a series of 'random' numbers ... use SRANDOM if you need to generate a set of truly random numbers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...