Gli ambienti di sviluppo rendono disponibili una o più funzioni per la generazione di numeri casuali
Reali | Interi | ||
BASIC | RND() | [0.0…1.0) | |
Calc Excel |
CASUALE() | [0.0…1.0) | |
CASUALE.TRA(INF; SUP) | [INF, SUP] | ||
GeoGebra | random() | [0.0…1.0) | |
Java Javascript |
Math.random() | [0.0…1.0) | |
Pascal | Random | [0.0…1.0) | |
Random(SUP) | [0, SUP-1] | ||
Python | random.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
Reali (sostituisci CASUALE() con la funzione specifica del linguaggio)
[0..1) | [0..SUP) | [0..SUP-INF) | [INF..SUP) | |
---|---|---|---|---|
Calc Excel |
CASUALE() | SUP*CASUALE() | (SUP-INF)*CASUALE() | (SUP-INF)*CASUALE()+INF |
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) |
Javascript | Math.floor(SUP*Math.random()) |
Math.floor(SUP*Math.random()+1) | Math.floor((SUP-INF+1)*Math.random()+INF) |
Pascal | Random(SUP) | Random(SUP)+1 | Random(SUP-INF+1)+INF |