mercredi 27 octobre 2021

How can I generate equally distributed random numbers in a range and with decimal places in bash?

How can I generate equally distributed random numbers in a range and with p.e, 2 or 5 decimal places in bash ?

Bash usually only supports whole numbers. However, numbers with decimal places can be used in bash, e.g. with the command sleep: sleep 1.23456 # sleep time in s

Given is the following example, which can generate with bash in a range from 0 to 10, equally distributed random numbers without decimal places.

ug_rnd=0
og_rnd=10

rnd="$((0x$(dd if=/dev/urandom of=/dev/stdout bs=4 count=1 status=none | xxd -p)))"
        my_rnd=$((rnd%(og_rnd-ug_rnd+1)+ug_rnd));        
        echo "$my_rnd"



Aucun commentaire:

Enregistrer un commentaire