防止后续随机结果在 Javascript 中重复
在我的网站上,我有一个按钮,可以从报价列表中选择随机报价并将随机选择的报价投影到文本框中。这是使用 JavaScript 完成的。
虽然我已经完成了这项工作,但我想要一段额外的代码来防止直接后续的引用与之前的引用相同。我希望以前的任何引用都能够再次出现,只是不直接出现在后面。
如果可能的话,我也希望这样,至少再点击 3 次,所使用的任何报价都不会再次出现 - 但这只是一个额外的好处。
无论如何,我目前拥有的代码如下:
<head>
<script language="javascript"><!--
function GenerateQuote(){var aquote=new Array;
aquote[0]="\"Quote0\"";
aquote[1]="\"Quote1\"";
aquote[2]="\"Quote2\""
aquote[3]="\"Quote3\"";
aquote[4]="\"Quote4\"";
aquote[5]="\"Quote5\"";
aquote[6]="\"Quote6\"";
rdmQuote=Math.floor(Math.random()*aquote.length);
document.getElementById("quoteBox").value=aquote[rdmQuote];
}
-->
</script>
</head>
<body>
<textarea id="quoteBox" readonly></textarea>
<button onClick="GenerateQuote()">Entertainment & Hobbies</button>
</body>
提前致谢;我相信这对你们这些聪明人来说不会太难!
On my website I have a button that selects a random quote from a list of quotes and projects the randomly selected quote into a text box. This is done using JavaScript.
Although I have this working, I'd like an additional piece of code that will prevent the directly subsequent quote being the same as the previous. I'd like any quote used to be able appear again however, just not directly following.
If possible I'd also like it so any quote used does not appear again for a minimum of another 3 clicks - but this would just be a bonus.
Anyway the code I currently have is as follows:
<head>
<script language="javascript"><!--
function GenerateQuote(){var aquote=new Array;
aquote[0]="\"Quote0\"";
aquote[1]="\"Quote1\"";
aquote[2]="\"Quote2\""
aquote[3]="\"Quote3\"";
aquote[4]="\"Quote4\"";
aquote[5]="\"Quote5\"";
aquote[6]="\"Quote6\"";
rdmQuote=Math.floor(Math.random()*aquote.length);
document.getElementById("quoteBox").value=aquote[rdmQuote];
}
-->
</script>
</head>
<body>
<textarea id="quoteBox" readonly></textarea>
<button onClick="GenerateQuote()">Entertainment & Hobbies</button>
</body>
Thanks in advance; I'm sure it won't be too hard for you brainiacs!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.sort()
方法,甚至更好,您可以查找Fisher-Yates
算法的 js 实现pop ()
在每次点击事件上的数组上,这样每次都会生成不同的报价,直到数组被完全消耗参考:
.sort()
method or even better you can look for a js implementation ofFisher-Yates
alghoritmpop()
over the array on each click event so you will generate every time a different quote until the array is fully consumedReference:
调用generateQuote() 27次的结果:
2,9,7,5,8,1,3,4,6,9,6,1,7,5,4,3,2,8,3, 1,6,5,2,7,9,4,8,2
正如你所看到的,在一个完整的循环之后,报价再次被打乱,并且如果最后一次出现相同的报价,则有可能出现相同的报价最后一个周期,并且是新周期中的第一个。它至少应该比我的 mp3 播放器中的播放列表随机播放要好得多:P
Results from calling generateQuote() 27 times:
2,9,7,5,8,1,3,4,6,9,6,1,7,5,4,3,2,8,3,1,6,5,2,7,9,4,8,2
As you can see, after a full cycle the quotes are shuffled again and there is a chance the same quote will appear if it was last in the last cycle and is first in the new cycle. It should be much better than playlist shuffling in my mp3 player at least :P
使用
在函数外部
,然后在函数内部使用
use
outside your function, then
inside your function
您可以调整 dontRepeatUntil 以满足您的需求
当然有更好的方法,但这个应该可行
you can tweak dontRepeatUntil to sweats your need
There's certainly better way but this one should work