javascript unique random numbers

前端 未结 3 1521
北海茫月
北海茫月 2021-01-28 02:16

experts

  • I wish to generate unique random numbers between two numbers (from text box of web page).
  • I am using array to store numbers. when user click butto
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 02:46

    Since I can't edit Crowder's answer, here is the simple unbiased way of scrambling an array:

    function scramble(nums){
        for (var n = nums.length; n; n--) {
            var x = Math.floor(Math.random() * n);
            var num = nums[n-1];
            nums[n-1] = nums[x];
            nums[x] = num;
        }
    }
    

提交回复
热议问题