English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Math.random()Il metodo restituisce un numero casuale decimale tra 0 e 1 (incluso 0, ma escludendo 1).
Math.random();Prova a vedere‹/›
Attenzione: Math.random()Restituisce sempre un numero decimale tra 0 e 1.
Math.random()Con Math.floor()Si può utilizzare insieme per restituire un numero intero casuale.
Questo esempio restituisce un numero intero casuale tra 0 e 9:
Math.floor(Math.random() * 10);Prova a vedere‹/›
Questo esempio restituisce un numero intero casuale tra 0 e 10:
Math.floor(Math.random() * 11);Prova a vedere‹/›
Questo esempio restituisce un numero intero casuale tra 1 e 10:
Math.floor((Math.random() * 10) + 1);Prova a vedere‹/›
Questo esempio restituisce un numero intero casuale tra 1 e 100:
Math.floor((Math.random() * 100) + 1);Prova a vedere‹/›
Questo esempio restituisce un numero intero casuale tra 11 e 20:
Math.floor((Math.random() * 10) + 11);Prova a vedere‹/›
Questo esempio restituisce un numero intero casuale tra 51 e 100:
Math.floor((Math.random() * 50) + 51);Prova a vedere‹/›
Questo esempio restituisce un numero casuale tra min (incluso) e max (escluso):
function getRandom(min, max) { return Math.floor(Math.random() * (max - min)) + min; }Prova a vedere‹/›
Questo esempio restituisce un numero casuale tra min e max (entrambi inclusi):
function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }Prova a vedere‹/›
Restituisci il numero casuale spostato del mouse:
Muovi il puntatore del mouse sul DIV seguente: