javascript唯一随机数
javascript unique random numbers
专家
- 我希望在两个数字之间生成唯一的随机数(来自网页的文本框(
- 我正在使用数组来存储数字。当用户点击按钮时,它会给出第一个随机数,并将其存储在数组中,当用户再次点击按钮时会生成随机数,将其与数组数进行比较,如果不同,则存储并显示
- 若显示最大可能的不同数字,则清除数组并通知用户
- 我已经编写了代码,但它给出了错误:堆栈溢出,或者有时会显示重复的结果
有人能揭示代码吗:
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);
}
}
我看不出该代码是如何生成堆栈溢出的(即使funShow
对funClick
有调用,funClick
对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(", ");
}
实例|源
这将数组排序作为一个起点,但随后还会在元素之间进行一系列随机交换。它仍然在恒定的时间内运行,但应该比单独使用数组排序有更好的结果。当然,您需要测试分布。
使用数组:
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
}
由于我无法编辑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;
}
}
相关文章:
- 如何在javascript中获得与特定数字相等的随机数
- Javascript动态变量(添加了随机数)
- 使用onkeyup JS事件检查输入的值是否唯一
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- Lodash映射并返回唯一
- 在javascript中添加和显示对象的随机数组中的视频
- 在数据库中循环值时,为输入框获取唯一值
- 设置随机数,然后每5秒随机减少一次,直到达到0(javascript)
- 调用的每个实例的唯一随机数
- 使用javascript生成唯一的随机数
- 生成的随机数是唯一的(无重复)
- 用唯一的随机数javascript填充数组
- 在javascript中生成10个介于1到10之间的唯一随机数
- 生成介于 0 和“x”之间的唯一随机数(整数)
- 在javascript中生成7个唯一的随机数
- 生成唯一的随机数执行插入,更新,删除显示到文本框
- 生成唯一的随机数 JavaScript
- 唯一随机数生成器问题
- javascript唯一随机数
- 唯一的随机数组javascript