Générer des nombres entiers aléatoires en JavaScript dans une gamme spécifique

Mots clés : javascriptrandomintegerjavascript

meilleur 5 Réponses Générer des nombres entiers aléatoires en JavaScript dans une gamme spécifique

vote vote

91

/**  * Returns a random number between min (inclusive) and max (exclusive)  */ function getRandomArbitrary(min, max) {     return Math.random() * (max - min) + min; }  /**  * Returns a random integer between min (inclusive) and max (inclusive).  * The value is no lower than min (or the next integer greater than min  * if min isn't an integer) and no greater than max (or the next integer  * lower than max if max isn't an integer).  * Using Math.round() will give you a non-uniform distribution!  */ function getRandomInt(min, max) {     min = Math.ceil(min);     max = Math.floor(max);     return Math.floor(Math.random() * (max - min + 1)) + min; } 
[0 .................................... 1) 
[0 .................................... 1) [min .................................. max) 
[0 .................................... 1) [min - min ............................ max - min) 
[0 .................................... 1) [0 .................................... max - min) 
                Math.random()                     | [0 .................................... 1) [0 .................................... max - min)                     |                     x (what we need) 
x = Math.random() * (max - min); 
x = Math.random() * (max - min) + min; 
min...min+0.5...min+1...min+1.5   ...    max-0.5....max └───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘   ← Math.round()    min          min+1                          max 
min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval) |        |        |         |        |        | └───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘   ← Math.floor()    min     min+1               max-1    max 
vote vote

90

var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; 
vote vote

71

function randomInteger(min, max) {   return Math.floor(Math.random() * (max - min + 1)) + min; } 
function randomNumber(min, max) {   return Math.random() * (max - min) + min; } 
// 0 -> 10 Math.floor(Math.random() * 11);  // 1 -> 10 Math.floor(Math.random() * 10) + 1;  // 5 -> 20 Math.floor(Math.random() * 16) + 5;  // -10 -> (-2) Math.floor(Math.random() * 9) - 10; 
vote vote

65

function getRandomizer(bottom, top) {     return function() {         return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;     } } 
var rollDie = getRandomizer( 1, 6 );  var results = "" for ( var i = 0; i<1000; i++ ) {     results += rollDie() + " ";    //make a string filled with 1000 random numbers in the range 1-6. } 
Math.random() * ( 1 + top - bottom ) 
Math.floor( Math.random() * ( 1 + top - bottom ) ) 
Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom 
vote vote

57

Math.floor((Math.random()*10) + 1);  
Math.floor((Math.random()*100) + 1) 

Questions similaires