【问题标题】:Javascript: Math.floor(Math.random()*array.length) not producing a random number?Javascript:Math.floor(Math.random()*array.length) 不产生随机数?
【发布时间】:2011-07-13 00:05:10
【问题描述】:
e 上的这个太棒了。
我有 while 循环来生成一个随机数,该随机数与之前生成的任何其他随机数都不相同。随机数用于从对象中选择文本值。
例如:
quoteArray[1] = "some text"
quoteArray[2] = "some different text"
quoteArray[3] = "text again"
quoteArray[4] = "completely different text"
quoteArray[5] = "ham sandwich"
这是一个更大函数的一部分,在该函数循环通过 = quoteArray.length 之后,它会重置并重新开始循环。我遇到的问题是以下代码有时会产生无限循环:
//Note: at this point in the function I have generated a random number once already and stored it in 'randomnumber'
//I use this while statement to evaluate 'randomnumber' until the condition of it NOT being a number that has already been used and NOT being the last number is met.
while(randomnumber === rotationArray[randomnumber] || randomnumber === lastnumber){
randomnumber = Math.floor(Math.random() * (quoteArray.length));
}
当我 console.log(randomnumber) - 当我陷入循环时 - 结果我只是得到'0'。当卡在循环中时,它看起来好像 Math.floor(Math.random() * (quoteArray.length)) 正在产生一个随机数,而只是无限地产生“0”。
谁能告诉我为什么会遇到这个问题?
编辑:这是完整的相关代码,带有函数 + 变量声明
// 初始化quoteObj的函数
函数quoteObj(文本,cname,ccompany,url,height){
this.text=文本;
this.cname=cname;
这个.ccompany=ccompany;
这个.url=url;
这个.高度=高度;
}
// 使用 XML 表中的报价信息填充我的报价对象。
var qObj = new quoteObj('','','','');
var quoteArray = new Array();
变量计数器 = 0;
//循环遍历每个 XML 项并将数据加载到一个对象中,然后将该对象存储在一个数组中
$.ajax({
类型:“获取”,
url: "quotes.xml",
数据类型:“xml”,
成功:函数(xml){
$(xml).find('quote').each(function(){
quoteArray[counter] = new quoteObj('','','','');
console.log(quoteArray[counter]);
quoteArray[counter].text = $(this).find('text').text();
quoteArray[counter].cname = $(this).find('customer_name').text();
quoteArray[counter].ccompany = $(this).find('customer_company').text();
quoteArray[counter].url = $(this).find('project').text();
++计数器;
});
}
});
// 这是产生我的无限循环问题的设置。
// 我已经包含了所有其他代码,以防人们想知道有关项目如何初始化等的具体事情。
// 生成一个随机的第一个引号,然后随机遍历整个集合并重新开始。
var randomnumber = Math.floor(Math.random() * (quoteArray.length));
var rotationArray = new Array(quoteArray.length);
变量 v = 0;
var lastnumber = -1;
bHeight = $('#rightbox').height() + 50;
变量 cHeight = 0;
var divtoanim = $('#customerquotes').parent();
//不相关的//
// 给内部阴影一个高度,以便溢出隐藏与引号一起使用。
$(divtoanim).css({'height' : bHeight});
// 随机旋转引用函数。
设置间隔(函数(){
随机数 = Math.floor(Math.random() * (quoteArray.length));
//检查函数循环是否需要从头开始。
if(v == (quoteArray.length)){
旋转数组.length = 0;
v = 0;
}
//确定随机数是否与之前生成的任何其他随机数不同并且与最后一个随机数不同
while(randomnumber === rotationArray[randomnumber] || randomnumber === lastnumber){
随机数 = Math.floor(Math.random() * (quoteArray.length));
}
lastnumber = 随机数;
旋转数组[随机数] = 随机数;
++v;
//不相关的//
//动画序列
$('#ctext, #cname').animate({'opacity':'0'},2000, function(){
$('#ctext').html(quoteArray[randomnumber].text);
$('#cname').html('-' + quoteArray[randomnumber].cname);
cHeight = $('#customerquotes').height() + 50;
调整高度(b高度,c高度,divtoanim);
$('#ctext').delay(500).animate({'opacity':'1'},500);
$('#cname').delay(1500).animate({'opacity':'1'},500);
});
},15000);
【问题讨论】:
-
-
-
直截了当地说,如果“quoteArray”没有用var quoteArray = []; 或var quoteArray = new Array(); 定义,那么它可能根本就不是一个数组。
-
rotationArray 是一个数组,存储每个循环产生的随机数。
-
quoteArray 声明如下: var quoteArray = new Array();
标签:
javascript
random
while-loop
infinite-loop