请问,如何为此添加随机函数? [英] Please, how to add a random function to this?

查看:88
本文介绍了请问,如何为此添加随机函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要随机选择一个数组项而不是

顺序。任何人

知道怎么样?非常感谢你。 Amy


< BODY onload =" changelink();">< a href ="#" id =" url">< / a>


var arr = [

[" Text"],[" http:/ /www.google.com"],

[" Text"],[" http://www.yahoo.com"]

];


var dynLink = null;


函数init(){

dynLink = document.getElementById(''url' ');

window.setTimeout(''changeURL(0)'',1000);

}


函数changeURL (i){

if(i> = arr.length){

i = 0;

}

dynLink.innerHTML = arr [i] [0];

dynLink.href = arr [i] [1];

window.setTimeout(''changeURL(' '+(++ i)+''''',1000);

}

window.onload = init;

解决方案



Amy D?D°D?D ??? D°D2:


函数changeURL(i){

if(i> = arr.length){

i = 0;

}

dynLink.inn erHTML = arr [i] [0];

dynLink.href = arr [i] [1];

window.setTimeout(''changeURL(''+( ++ i)+''''',1000);

}



更改为:

i = Math.round(Math.random()* arr.length)


marss写道:


[ snip]


i = Math.round(Math.random()* arr.length)



Rounding创造了错误的范围。考虑:


Math.round(Math.random()* 2);


预期的可能结果集是{0,1 }。然而,当x


> = 0.75时,2x> = 1.5。 x的值(随机方法的结果)将超过



0.75,以及1.5或更大的回合,最多为2.


此外,舍入会产生不均匀的分布,其中第一个和最后一个数字将以其他人的频率的一半发生。


使用发言方法:


Math.floor(Math.random()* arr.length);


Mike


天哪,但是我不知道如何将它放入功能中。

有人可以帮助我吗?

Michael Winter写道:


marss写道:


[snip]


i = Math。 round(Math.random()* arr.length)



舍入会创建错误的范围。考虑:


Math.round(Math.random()* 2);


预期的可能结果集是{0,1 }。然而,当x


> = 0.75时,2x> = 1.5。 x的值(随机方法的结果)将超过



0.75,以及1.5或更大的回合,最多为2.


此外,舍入会产生不均匀的分布,其中第一个和最后一个数字将以其他人的频率的一半发生。


使用发言方法:


Math.floor(Math.random()* arr.length);


Mike


Hello, I need to make this select an array item randomly instead of in
order. Anyone
know how? Thank you very much. Amy

<BODY onload="changelink();"><a href="#" id="url"></a>

var arr = [
["Text"],["http://www.google.com"],
["Text"],["http://www.yahoo.com"]
];

var dynLink = null;

function init() {
dynLink = document.getElementById(''url'');
window.setTimeout(''changeURL(0)'',1000);
}

function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[i][0];
dynLink.href = arr[i][1];
window.setTimeout(''changeURL(''+(++i)+'')'',1000);
}
window.onload = init;

解决方案


Amy D?D°D?D???D°D2:

function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[i][0];
dynLink.href = arr[i][1];
window.setTimeout(''changeURL(''+(++i)+'')'',1000);
}

change to:
i = Math.round(Math.random()* arr.length)


marss wrote:

[snip]

i = Math.round(Math.random()* arr.length)

Rounding creates the wrong range. Consider:

Math.round(Math.random() * 2);

The intended set of possible results is {0,1}. However, 2x >= 1.5 when x

>= 0.75. The value of x (the result of the random method) will exceed

0.75, and 1.5 or greater rounds up to 2.

Moreover, rounding creates an uneven distribution where the first and
last numbers will occur with half the frequency of the others.

Use the floor method:

Math.floor(Math.random() * arr.length);

Mike


Gosh thanks but I have no idea how to put it in the function instead.
Can someone help me?
Michael Winter wrote:

marss wrote:

[snip]

i = Math.round(Math.random()* arr.length)


Rounding creates the wrong range. Consider:

Math.round(Math.random() * 2);

The intended set of possible results is {0,1}. However, 2x >= 1.5 when x

>= 0.75. The value of x (the result of the random method) will exceed

0.75, and 1.5 or greater rounds up to 2.

Moreover, rounding creates an uneven distribution where the first and
last numbers will occur with half the frequency of the others.

Use the floor method:

Math.floor(Math.random() * arr.length);

Mike


这篇关于请问,如何为此添加随机函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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