mercredi 24 février 2021

bash, How to do a given function more random by adding a seed?

How to do given random function, more random by adding a seed ?

The question is related on Linux like Debian or Ubuntu, bash and a given function which use RANDOM.

Given function which should be more random:

getRND(){
    min="${1:-1}"   ## min is the first parameter, or 1 if no parameter is given           
    max="${2:-100}" ## max is the second parameter, or 100 if no parameter is given
    rnd_count=$((RANDOM%(max-min+1)+min));
    echo "$rnd_count"
}

var=$(getRND -10 10) # Call the function
echo $var # output

Ideas for a seed:

  • use actual date and time (the time in ms if possible) and doing a shuff with this and use it as seed
  • if one other seed create a more random random, use the better one



Aucun commentaire:

Enregistrer un commentaire