如何为在线测验创建一系列唯一随机数 [英] How to create an array of unique random numbers for an online quiz

查看:62
本文介绍了如何为在线测验创建一系列唯一随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候


我正在制作一个flashcard类型的应用程序来帮助我进行TCP / IP

协议测试。

我的讲师将定期测试我们的设备或网络功能如何与OSI层相关联。例如。 bits-layer 1.

无论如何,我希望测验能在每次拍摄时对问题进行重新排序。


以下是我所做代码的一部分到目前为止,测验中有62个组件。

var slot = new Array(62);

var test = 0;


for(n = 0; n< 62; n ++)

{

slot [n] = 99;

}


do

{

test = Math.floor(62 * Math.random());

if (slot [num] == 99)

{

slot [num] = test;

num ++;

}

} while(num< 62);


}

问题是,我没有得到每个插槽中的数字不同。我在这里缺少什么?
另外,有没有更有效的方法呢?

allen

Greetings

I am making a flashcard type application to help me in my TCP/IP
protocols test.
My instructor will test us periodically on how a device or networking
function relates to the OSI layer. EG. bits-layer 1.
Any way, I want the quiz to reorder the problems each time I take it.

Here is part of the code i did so far for 62 components in the quiz.
var slot=new Array(62);
var test=0;

for(n=0;n<62;n++)
{
slot[n]=99;
}

do
{
test=Math.floor(62*Math.random());
if (slot[num]==99)
{
slot[num]=test;
num++;
}
} while (num<62);

}
The problem is, I don''t get a different number in each slot. What am I
missing here. Also, is there a more efficient way to do this?
allen

推荐答案

27/05 / 2005 18:35,alanbe写道:


[snip]
On 27/05/2005 18:35, alanbe wrote:

[snip]
任何方式,我希望测验每次重新排序问题接受它。
Any way, I want the quiz to reorder the problems each time I take it.




最好将一个预先填充的数组洗牌。


var slot = [];


for(var i = 0; i< 62; ++ i){

slot [i] = i;

}


slot.shuffle();


其中shuffle方法定义如下:


Array.prototype.swap = function(i,j){

var t = this [i];这[i] =这[j];这[j] = t;

};


Array.prototype.shuffle = function(){

var i = this.length;


while(i - ){

this.swap(i,Math.floor((Math.random()%1) *(i + 1)));

}

};


[snip]

Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



It would probably be best to shuffle a pre-filled array.

var slot = [];

for(var i = 0; i < 62; ++i) {
slot[i] = i;
}

slot.shuffle();

where the shuffle method is defined by:

Array.prototype.swap = function(i, j) {
var t = this[i]; this[i] = this[j]; this[j] = t;
};

Array.prototype.shuffle = function() {
var i = this.length;

while(i--) {
this.swap(i, Math.floor((Math.random() % 1) * (i + 1)));
}
};

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


谢谢迈克尔。这对我有帮助。还必须返回并研究与数组相关的所有

方法。
http://4umi.com/web/javascript/array.htm

我对JavaScript的强大感到惊讶。


我在发布问题后也看到了我的代码出错的地方。


alanbe

Thanks Michael. This helps me. Also had to go back and study all the
methods associated with arrays.
http://4umi.com/web/javascript/array.htm
I am constantly surprised at the power of JavaScript.

I also saw where my code went wrong after I posted the question.

alanbe


JRS:在文章< rP ***************** @ text.news.blueyonder.co.uk> ,日期

星期五,2005年5月27日18:39:19,在新闻中看到:comp.lang.javascript,Michael

Winter< m。***** *@blueyonder.co.invalid>发布:
JRS: In article <rP*****************@text.news.blueyonder.co.uk> , dated
Fri, 27 May 2005 18:39:19, seen in news:comp.lang.javascript, Michael
Winter <m.******@blueyonder.co.invalid> posted :
最好将一个预先填充的阵列洗牌。
It would probably be best to shuffle a pre-filled array.




它可能会更好;这可能不是最好的,当数组要用
填充连续数字时。


在这种情况下,可以直接处理数字。这取消了

加载循环,并略微简化了排列循环。


除非当然需要在其他地方进行随机播放并且更多

关注代码大小而不是速度。


常见问题4.22 HTM,蓝色方框,请参阅。


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/> JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,常见问题项目,链接。



It could probably be better; it could not be best, when the array is to
be filled with consecutive numbers.

In that case, one can deal the numbers directly. That removes the
loading loop, and slightly simplifies the arranging loop.

Unless of course one requires the shuffle routine elsewhere and is more
concerned about code size than speed.

Faq 4.22 HTM, blue box, refers.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


这篇关于如何为在线测验创建一系列唯一随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆