Javascript: Random number out of 5, no repeat until all have been used(Javascript:5 中的随机数,在全部使用之前不重复)
问题描述
我正在使用下面的代码为我页面上的每个图像分配一个随机类(共五个).
I am using the below code to assign a random class (out of five) to each individual image on my page.
$(this).addClass('color-' + (Math.floor(Math.random() * 5) + 1));
它工作得很好,但我想做到这一点,这样就不会连续出现两个相同的班级.
It's working great but I want to make it so that there are never two of the same class in a row.
如果连续没有两个相同的类会更好,并且在所有 5 个都被使用之前它也不会多次使用任何类...如,从数组中删除每个使用的类,直到都用完了,再重新开始,不允许前5个最后一个和后5个第一个颜色一样.
Even better would be if there were never two of the same in a row, and it also did not use any class more than once until all 5 had been used... As in, remove each used class from the array until all of them have been used, then start again, not allowing the last of the previous 5 and the first of the next 5 to be the same color.
希望这是有道理的,并提前感谢您的帮助.
Hope that makes sense, and thanks in advance for any help.
推荐答案
您需要创建一个包含可能值的数组,并且每次从数组中检索随机索引以使用其中一个值时,将其从数组.
You need to create an array of the possible values and each time you retrieve a random index from the array to use one of the values, you remove it from the array.
这是一个通用随机函数,在使用所有值之前不会重复.您可以调用它,然后将此索引添加到类名的末尾.
Here's a general purpose random function that will not repeat until all values have been used. You can call this and then just add this index onto the end of your class name.
var uniqueRandoms = [];
var numRandoms = 5;
function makeUniqueRandom() {
// refill the array if needed
if (!uniqueRandoms.length) {
for (var i = 0; i < numRandoms; i++) {
uniqueRandoms.push(i);
}
}
var index = Math.floor(Math.random() * uniqueRandoms.length);
var val = uniqueRandoms[index];
// now remove that value from the array
uniqueRandoms.splice(index, 1);
return val;
}
工作演示:http://jsfiddle.net/jfriend00/H9bLH/
所以,你的代码应该是这样的:
So, your code would just be this:
$(this).addClass('color-' + (makeUniqueRandom() + 1));
<小时>
这是一种面向对象的形式,它允许在您的应用中的不同位置使用其中的多个:
Here's an object oriented form that will allow more than one of these to be used in different places in your app:
// if only one argument is passed, it will assume that is the high
// limit and the low limit will be set to zero
// so you can use either r = new randomeGenerator(9);
// or r = new randomGenerator(0, 9);
function randomGenerator(low, high) {
if (arguments.length < 2) {
high = low;
low = 0;
}
this.low = low;
this.high = high;
this.reset();
}
randomGenerator.prototype = {
reset: function() {
this.remaining = [];
for (var i = this.low; i <= this.high; i++) {
this.remaining.push(i);
}
},
get: function() {
if (!this.remaining.length) {
this.reset();
}
var index = Math.floor(Math.random() * this.remaining.length);
var val = this.remaining[index];
this.remaining.splice(index, 1);
return val;
}
}
示例用法:
var r = new randomGenerator(1, 9);
var rand1 = r.get();
var rand2 = r.get();
工作演示:http://jsfiddle.net/jfriend00/q36Lk4hk/
这篇关于Javascript:5 中的随机数,在全部使用之前不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript:5 中的随机数,在全部使用之前不重复
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01