0

专家

  • 我希望在两个数字之间生成唯一的随机数(来自网页的文本框)。
  • 我正在使用数组来存储数字。当用户单击按钮时,它会给出第一个随机数,将其存储在数组中,当用户再次单击按钮时,它会生成随机数,并将其与数组数进行比较,如果不同,则将其存储并显示出来。
  • 如果显示最大可能的不同数字,则清除数组并通知用户。
  • 我已经编写了代码,但它给出了错误:堆栈溢出,或者有时我会显示重复的结果。

任何人都可以阐明代码:

var allnums = new Array();
var num1= new Number;
var num2= new Number;

function funClick()
{
  var num1 = Number(document.getElementById('lnum').value); 
  var num2 = Number(document.getElementById('hnum').value); 

  if (allnums.length==num2)
  {
    alert("Maximum non-duplicate numbers served. Now resetting the counter.");
    allnums = [];
    return;
  }

  if (num1<num2) 
  {
    x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
    funShow(x);
  }
  else
  {
    alert("You entered wrong number criteria!");  
  }
}

function funShow(x)
{
  var bolFound = false;
  for (var i=0;i<allnums.length;i++)
  {
    if((allnums[i])==x)
    {   
      funClick();
    }    
  }
  if (bolFound == false)
  {
    document.getElementById('rgen').innerText  = x;
    allnums.push(x);
  }
}
4

3 回答 3

2

我看不到该代码是如何产生堆栈溢出的(即使funShow调用funClickfunClick调用funShow,funShow的调用funClick也不应该因为逻辑错误而发生 - 修复错误,你会得到堆栈溢出,虽然),但它有几个问题。见评论:

// style: Use [], not new Array()
var allnums = new Array();

// `new Number` doesn't do anything useful here
var num1 = new Number;
var num2 = new Number;

function funClick() {
    // For user-entered values, use parseInt(value, 10) to parse them into numbers
    var num1 = Number(document.getElementById('lnum').value);
    var num2 = Number(document.getElementById('hnum').value);

    if (allnums.length == num2) {
        alert("Maximum non-duplicate numbers served. Now resetting the counter.");
        allnums = [];
        return;
    }

    // & is a bitwise AND operation, not a logical one. If your goal is to see
    // if both numbers are !0, though, it works but is obtuse.
    // Also, there is no ltnum2 variable anywhere, so trying to read its value
    // like this should be throwing a ReferenceError.
    if (num1 & ltnum2) {
        // You're falling prey to The Horror of Implicit Globals, x has not
        // been declared.
        x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
        funShow(x);
    } else {
        alert("You entered wrong number criteria!");
    }
}

function funShow(x) {
    var bolFound = false;
    // Again, & is a bitwise AND operation. This loop will never run, because
    // you start with 0 and 0 & anything = 0
    // But it should be throwing a ReferenceError, as there is no ltallnums
    // anywhere.
    for (var i = 0; i & ltallnums.length; i++) {
        if ((allnums[i]) == x) {
            funClick();
        }
    }
    // This condition will always be true, as you've done nothing to change
    // bolFound since you set it to false
    if (bolFound == false) {
        document.getElementById('rgen').innerText = x;
        allnums.push(x);
    }
}

有两种方法可以解决这个问题。这基本上就是您想要做的,但没有递归:

function funClick() {
    var num1 = parseInt(document.getElementById('lnum').value, 10);
    var num2 = parseInt(document.getElementById('hnum').value, 10);
    var nums = [];
    var targetCount;
    var x;

    // Check the inputs
    if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
        alert("Please ensure that hnum is higher than lnum and both are really numbers.");
        return;
    }

    // Find out how many integers there are in the range num1..num2 inclusive
    targetCount = num2 - num1 + 1;

    // Produce that many random numbers
    while (nums.length < targetCount) {
        x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
        if (nums.indexOf(x) < 0) {
            nums.push(x);
        }
    }

    // Show the result
    document.getElementById('rgen').innerText = nums.join(", ");
}

实例| 来源

这样做的问题是,填充最后几个插槽可能需要很长时间,因为我们必须随机命中它们。

另一种方法是按顺序生成具有数字的数组,然后将其弄乱。对于大范围,它可以显着提高效率。像这样的东西:

function funClick() {
    var num1 = parseInt(document.getElementById('lnum').value, 10);
    var num2 = parseInt(document.getElementById('hnum').value, 10);
    var nums = [];
    var x;

    // Check the inputs
    if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
        alert("Please ensure that hnum is higher than lnum and both are really numbers.");
        return;
    }

    // Create an array with those numbers in order
    for (x = num1; x <= num2; ++x) {
        nums.push(x);
    }

    // Sort it with a random comparison function
    nums.sort(function(a, b) {
        return 0.5 - Math.random();
    });

    // Show the result
    document.getElementById('rgen').innerText = nums.join(", ");
}

实例| 来源

但是,仅nums.sort(...)随机执行一次可能不会成功地产生随机结果;有关更多信息,请参阅本文(感谢eBusiness提供的链接以及他对以下内容的投入。)

因此,您可能想要更进一步并投入更多的随机操作。这是另一个例子:

function funClick() {
    var num1 = parseInt(document.getElementById('lnum').value, 10);
    var num2 = parseInt(document.getElementById('hnum').value, 10);
    var nums = [];
    var n, x, y;
    var num;

    // Check the inputs
    if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
        alert("Please ensure that hnum is higher than lnum and both are really numbers.");
        return;
    }

    // Create an array with those numbers in order
    for (n = num1; n <= num2; ++n) {
        nums.push(n);
    }

    // We only need to shuffle it if it's more than one element long
    if (nums.length > 1) {
        // Sort it "randomly"
        nums.sort(function(a, b) {
            return 0.5 - Math.random();
        });

        // Throw a bunch of random swaps in there
        for (n = 0; n < nums.length; ++n) {
            do {
                x = Math.floor(Math.random() * nums.length);
            }
            while (x === n);
            num = nums[x];
            nums[x] = nums[n];
            nums[n] = num;
        }
    }

    // Show the result
    document.getElementById('rgen').innerText = nums.join(", ");
}

实例| 来源

这会将数组排序作为起点,但随后也会在元素之间进行一堆随机交换。它仍然在恒定时间内运行,但应该比单独使用数组排序有更好的结果。自然,您会想要测试分布。

于 2013-01-12T08:19:08.873 回答
2

使用数组:

var uniqueRandomNumbers = new Array();
var totalNumbers = 100;
for (var i=0; i<totalNumbers; i++){
    uniqueRandomNumbers.push(i);
}
uniqueRandomNumbers.sort(function() {return 0.5 - Math.random();});
var uniqueNumber;
for(var i=0; i<uniqueRandomNumbers.length; i++){
    uniqueNumber = uniqueRandomNumbers[i];
    //do something with the number
}
于 2013-07-13T18:18:22.167 回答
0

由于我无法编辑 Crowder 的答案,因此这里是加扰数组的简单无偏方法:

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;
    }
}
于 2013-01-12T22:11:07.603 回答