我需要随机抽出2至5个,每个时间都是不同的。 例如,我不想一行两把三 s,一行两把五.。 因此,我不得不把这引向(a) lo。
for(var i = 0; i < noBoxes.length; i+=randNumber) {
//....
}
我如何这样做?
我需要随机抽出2至5个,每个时间都是不同的。 例如,我不想一行两把三 s,一行两把五.。 因此,我不得不把这引向(a) lo。
for(var i = 0; i < noBoxes.length; i+=randNumber) {
//....
}
我如何这样做?
a) 随机数量等于零-1,并添加到原体范围(因为母体不是0):
i = some random int from 2 to 5
delta = randInt(3) // range of possible values from 2 to 5 is 4, minus 1 to
// prevent getting all the way round to i again
nextval = (i-2+delta)%4+2 // shift i down by the minimum, add the
// delta and modulo the range
这样做是因为它比范围增加1个,因此它永远不会回到原来的数字。 例如,i=3,随机为0至2,因此最高比率为(i-2+2)%3+2=3%+2=0+2=2。
function differentRandInt(min,max,current) {
var shiftedcurrent = current-min;
var range = max-min+1;
var delta = Math.floor((Math.random()*(range-1))+1);
return (shiftedcurrent + delta)%range + min;
}
因此,如果i=3,即i-min为1,那么增加三角洲后的范围为2,3,4,模块4, 产量为23,0,因此添加斜体使我们有4,5,2。
Edit: more complete solution, and fixed off-by-one error:
var prev, randNumber;
for(var i = 0; i < noBoxes.length; i+=randNumber) {
do {
randNumber = 2 + Math.floor(Math.random() * 4);
} while(randNumber === prev);
prev = randNumber;
}
与此类似:。
var loopingValues = [2, 3, 4, 5],
len,
value,
rand;
while ( (len=loopingValues.length) ) {
rand = ~~( ( Math.random() % 1 ) * len);
value = loopingValues.splice(rand, 1)[0];
//alert( value )
}
现在,你可以随意使用<条码>价值<>t/code>,作为<条码>2、3、4和5。
http://jsbin.com/ejizuw/rel=“nofollow”http://jsbin.com/ejizuw/。
var loopingValues = [2, 3, 4, 5],
value,
rand;
while ( loopingValues.length ) {
rand = Math.random();
if ( rand === 1 ) {
rand = 0;
}
rand = rand * loopingValues.length;
rand = Math.floor(rand);
value = loopingValues[rand];
loopingValues.splice(rand, 1);
alert(value);
}
你可以在你开始娱乐之前,把你包成一阵。
var noBoxes={length:100};
var R= [2], r= 2, n= 2;
while(R.length<noBoxes.length){
n= R[R.length-1];
while(r== n) r= 2+Math.floor(Math.random()*4)
R.push(r);
}
And then loop through the array-
for(var i = 0; i < noBoxes.length; i+=R[i]) {
....
}
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.