Numeri pseudocasuali – Funzioni

Gli ambienti di sviluppo rendono disponibili una o più funzioni per la generazione di numeri casuali

RealiInteri
BASICRND()[0.0…1.0) 
Calc / ExcelCASUALE()[0.0…1.0)
CASUALE.TRA(INF; SUP)[INF, SUP]
GeoGebrarandom()[0.0…1.0)
Java / JavascriptMath.random()[0.0…1.0) 
PascalRandom[0.0…1.0)
Random(SUP)[0, SUP-1]
Pythonrandom.random()[0.0…1.0)
random.uniform(INF,SUP)[INF, SUP)
random.randint(INF,SUP)[INF, SUP]

Numeri in un intervallo

A partire dalla funzione di base si possono ottenere numeri distribuiti uniformemente in un intervallo a piacere.

Numeri reali (sostituisci CASUALE() con la funzione specifica del linguaggio)

[0..1)[0..SUP)[0..SUP-INF)[INF..SUP)
Calc / ExcelCASUALE()SUP*CASUALE()(SUP-INF)*CASUALE()(SUP-INF)*CASUALE()+INF

Numeri interi

[0, SUP-1][1, SUP][INF, SUP]
Java(int)(SUP*Math.random())
(int)(SUP*Math.random()+1)(int)((SUP-INF+1)
*Math.random()+INF)
JavascriptMath.floor(SUP*Math.random())
Math.floor(SUP*Math.random()+1)Math.floor((SUP-INF+1)
*Math.random()+INF)
PascalRandom(SUP)Random(SUP)+1Random(SUP-INF+1)+INF

Prova!

RealiInteri
Math.random()Math.floor(…)
SUP*Math.random()
SUP*Math.random()+1
(SUP-INF)*Math.random()+INF
(SUP-INF+1)*Math.random()+INF
INF
SUP