jeudi 31 décembre 2020

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?




Aucun commentaire:

Enregistrer un commentaire