# 在0到'x'之间生成唯一的随机数（整数）

``var limit = 10; var amount = 3;` `

• `Math.random()`返回0到1之间的随机数（包括0，不包括1）。
• 将这个数字乘以所需的最高数字（例如10）
• 围绕这个数字

` `Math.round(Math.random()*10) + 1` `

` `//Example, including customisable intervals [lower_bound, upper_bound) var limit = 10, amount = 3, lower_bound = 1, upper_bound = 10, unique_random_numbers = []; if (amount > limit) limit = amount; //Infinite loop if you want more unique //Natural numbers than existemt in a // given range while (unique_random_numbers.length < limit) { var random_number = Math.round(Math.random()*(upper_bound - lower_bound) + lower_bound); if (unique_random_numbers.indexOf(random_number) == -1) { // Yay! new random number unique_random_numbers.push( random_number ); } } // unique_random_numbers is an array containing 3 unique numbers in the given range` `
` `Math.floor(Math.random() * (limit+1))` `

Math.random（）生成一个0到1之间的浮点数，Math.floor（）将其舍入到一个整数。

` `Math.floor(Math.random() * (num2-num1 + 1) + num1)` `

` `function generateRange(pCount, pMin, pMax) { min = pMin < pMax ? pMin : pMax; max = pMax > pMin ? pMax : pMin; var resultArr = [], randNumber; while ( pCount > 0) { randNumber = Math.round(min + Math.random() * (max - min)); if (resultArr.indexOf(randNumber) == -1) { resultArr.push(randNumber); pCount--; } } return resultArr; }` `

` `Math.floor(Math.random()*limit)+1` `

` `var limit = 10; var amount = 3; var nums = new Array(); for(int i = 0; i < amount; i++) { var add = true; var n = Math.round(Math.random()*limit + 1; for(int j = 0; j < limit.length; j++) { if(nums[j] == n) { add = false; } } if(add) { nums.push(n) } else { i--; } }` `
` `for(i = 0;i <amount; i++) { var randomnumber=Math.floor(Math.random()*limit)+1 document.write(randomnumber) }` `

1. 生成从0到x的所有数字的数组
2. 随机sorting数组，使元素随机排列
3. 挑第一个

` `// assuming _ is the Lodash library // generates `amount` numbers from 0 to `upperLimit` inclusive function uniqueRandomInts(upperLimit, amount) { var possibleNumbers = _.range(upperLimit + 1); var shuffled = _.shuffle(possibleNumbers); return shuffled.slice(0, amount); }` `

` `var RandomSequenceOfUnique = (function() { function RandomSequenceOfUnique(seedBase, seedOffset) { var prime = 4294967291, residue, permuteQPR = function(x) { if (x >= prime) return x; residue = (x * x) % prime; return (x <= prime / 2) ? residue : prime - residue; } this.next = function() { return permuteQPR((permuteQPR(this.index++) + this.intermediateOffset) ^ 0x5bf03635); } this.index = permuteQPR(permuteQPR(seedBase) + 0x682f0161); this.intermediateOffset = permuteQPR(permuteQPR(seedOffset) + 0x46790905); } return RandomSequenceOfUnique; }());` `

` `var generator = new RandomSequenceOfUnique(Date.now(), parseInt(Math.random() * 10000));` `

` ` var num = generator.next();` `

` `var randomNums = function(amount, limit) { var result = [], memo = {}; while(result.length < amount) { var num = Math.floor((Math.random() * limit) + 1); if(!memo[num]) { memo[num] = num; result.push(num); }; } return result; }` `

` `/** * Generates an array with numbers between * min and max randomly positioned. */ function genArr(min, max, numOfSwaps){ var size = (max-min) + 1; numOfSwaps = numOfSwaps || size; var arr = Array.apply(null, Array(size)); for(var i = 0, j = min; i < size & j <= max; i++, j++) { arr[i] = j; } for(var i = 0; i < numOfSwaps; i++) { var idx1 = Math.round(Math.random() * (size - 1)); var idx2 = Math.round(Math.random() * (size - 1)); var temp = arr[idx1]; arr[idx1] = arr[idx2]; arr[idx2] = temp; } return arr; } /* generating the array and using it to get 3 uniques numbers */ var arr = genArr(1, 10); for(var i = 0; i < 3; i++) { console.log(arr.pop()); }` `