It is required to generate an array containing six random numbers, and these random numbers cannot be repeated.
(The array contains multiple random numbers, each random number is six digits, and the random numbers in the array are not repeated)
You can refer to the following two codes and combine the two.
//隨機(jī)六位數(shù)
function MathRand()
{
var Num="";
for(var i=0;i<6;i++)
{
Num+=Math.floor(Math.random()*10);
}
}
//不重復(fù)隨機(jī)數(shù)組
/* num 要產(chǎn)生多少個(gè)隨機(jī)數(shù)
from 產(chǎn)生隨機(jī)數(shù)的最小值
to 產(chǎn)生隨機(jī)數(shù)的最大值 */
function createRandom(num ,from ,to )
{
var arr=[];
for(var i=from;i<=to;i++)
arr.push(i);
arr.sort(function(){
return 0.5-Math.random();
});
arr.length=num;
return arr;
}
function createRandom2(num , from , to)
{
var arr=[];
var json={};
while(arr.length<num)
{
//產(chǎn)生單個(gè)隨機(jī)數(shù)
var ranNum=Math.ceil(Math.random()*(to-from))+from;
//通過判斷json對(duì)象的索引值是否存在 來標(biāo)記 是否重復(fù)
if(!json[ranNum])
{
json[ranNum]=1;
arr.push(ranNum);
}
}
return arr;
}
alert(createRandom2(10,0,50));//生成10個(gè)從0-50之間不重復(fù)的隨機(jī)數(shù)
This can be done through a recursion. For the convenience of demonstration, I changed it to generate a set of non-repeating positive integer random numbers within 10.
The main method it relies on is the indexOf() method, which is used to find the index of a certain value in the array. If it is not found in the array, -1 is returned.
The code is as follows:
var arr=[];
function getRandom(){
var random=Math.floor(Math.random()*1000000);
console.log(random);
//判斷生成的數(shù)在數(shù)組中是否存在,判斷是否是6位數(shù)
//如果不存在而且是6位數(shù),放入數(shù)組
if(random.toString().length==6&&arr.indexOf(random)==-1){
arr.push(random)
}else{
//如果存在或者不是6位數(shù),接著調(diào)用這個(gè)函數(shù),生成滿足要求的隨機(jī)數(shù)
console.log("不符合要求的"+random)
getRandom();
}
}
for(var i=0;i<6;i++){
getRandom();
}
console.log(arr);
Time will not repeat, and using timestamps to generate random numbers will not repeat either.
console.log((Math.random() * Date.now()).toFixed(0));
// 6位的
console.log((Math.random() * Date.now() / 1000000).toFixed(0));
And to a certain extent, there is no absolute non-repeating random number in this world, and all combinations are not infinite. Even if all the generated random numbers are stored, and then compared when generated, if they are found to already exist, they will be generated. New random numbers will eventually form an infinite loop when all combinations have been tried. What's more, it's only 6 digits, and there are only 472,392 combinations in total.
Let me give you a simple and easy-to-use one. The above cannot guarantee that it will never be repeated. If it is repeated, remember to buy a lottery ticket. Math.random().toString(36).slice(2,8)
Non-repetitive random number sequence generation algorithm
I saw an article that is very clever. The results and efficiency can be guaranteed. Use code + comments to implement it:
function getRandom(numCount) {
var numList = [];
var numMin = 100000;
var numMax = 999999;
var listLen = numMax - numMin + 1;
var outPut = [];
// 將所有數(shù)順序裝填到數(shù)字列表中
for (var i = numMin; i < numMax + 1; i++) {
numList.push(i);
}
var randNum;
for (var j = 0; j < numCount; j++) {
// 產(chǎn)生一個(gè)小于列表長(zhǎng)度的隨機(jī)數(shù)randNum
randNum = Math.floor(Math.random() * listLen);
// 將列表的第randNum項(xiàng)輸出
outPut.push(numList[randNum]);
// 將列表的最后一項(xiàng)替換到剛被取出的數(shù)的位置
numList[randNum] = numList[listLen - 1];
// 列表長(zhǎng)度減一,即列表最后一項(xiàng)不會(huì)再被取到;
listLen--;
}
return outPut;
}
var arr = getRandom(30);
console.log(arr);
JS is not well written (escape
Write a loop to generate a random number each time and throw it into the set. When the set is long enough, it will be converted into an array and returned
If the required number in the array is small, you can use the array method to determine it
console.time('time:');
function createRandomArr(l){
var r = [];
var o = {};
var a;
for (var i = 0;i < l;i++){
a = Math.random().toString().slice(2,8);
o[a] ? i-- : (r.push(a),o[a] = true);
}
return r;
}
var res = createRandomArr(10000);
console.log(res,res.length);
console.timeEnd("time:");