Want to produce random numbers between 1-45 without repetition

前端 未结 4 1467
你的背包
你的背包 2021-01-29 13:53

I have come across a very strange problem. I have tried to find its solution but in vain. My problem is that I want to create a random number between 1-45 and I don\'t want tha

4条回答
  •  渐次进展
    2021-01-29 14:05

    Random selection, by definition, will repeat randomly.

    However, you can build an array containing each of your numbers and then shuffle the array, producing a random order of numbers without repetition.

    var nums = [], i;
    for( i=1; i<=45; i++) nums.push(i);
    nums.sort(function(a,b) {return Math.random()-0.5;});
    alert(nums.join(","));
    

提交回复
热议问题