11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5) let "tot_sei += 1";;<br />

esac<br />

}<br />

echo<br />

while [ "$lanci" -lt "$NUMMAX_LANCI" ]<br />

do<br />

let "dado1 = RANDOM % $FACCE"<br />

aggiorna_contatori $dado1<br />

let "lanci += 1"<br />

done<br />

visualizza_risultati<br />

exit 0<br />

Capitolo 9. Variabili riviste<br />

# I punteggi dovrebbero essere <strong>di</strong>stribuiti abbastanza equamente, nell’ipotesi<br />

#+ che RANDOM sia veramente casuale.<br />

# Con $NUMMAX_LANCI impostata a 600, la frequenza <strong>di</strong> ognuno dei sei numeri<br />

#+ dovrebbe aggirarsi attorno a 100, più o meno 20 circa.<br />

#<br />

# Ricordate che RANDOM è un generatore pseudocasuale, e neanche<br />

#+ particolarmente valido.<br />

# La casualità è un argomento esteso e complesso.<br />

# Sequenze "casuali" sufficientemente lunghe possono mostrare<br />

#+ un andamento caotico e "non-casuale".<br />

# Esercizio (facile):<br />

# ------------------<br />

# Riscrivete lo script per simulare il lancio <strong>di</strong> una moneta 1000 volte.<br />

# Le possibilità sono "TESTA" o "CROCE".<br />

Come si è visto nell’ultimo esempio, è meglio “ricalcolare il seme” del generatore RANDOM ogni volta<br />

che viene invocato. Utilizzando lo stesso seme, RANDOM ripete le stesse serie <strong>di</strong> numeri. 6 (Rispecchiando<br />

il comportamento della funzione random() del C.)<br />

Esempio 9-28. Cambiare il seme <strong>di</strong> RANDOM<br />

#!/bin/bash<br />

# see<strong>di</strong>ng-random.sh: Cambiare il seme della variabile RANDOM.<br />

MAX_NUMERI=25 # Quantità <strong>di</strong> numeri che devono essere generati.<br />

numeri_casuali ()<br />

{<br />

contatore=0<br />

while [ "$contatore" -lt "$MAX_NUMERI" ]<br />

do<br />

numero=$RANDOM<br />

154

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!