数学随机数,不重复前一个数字
似乎找不到这个问题的答案,假设我有这个:
setInterval(function() {
m = Math.floor(Math.random()*7);
$('.foo:nth-of-type('+m+')').fadeIn(300);
}, 300);
如何使随机数不会重复自身。例如,如果随机数是2,我不希望再次出现2。
Can't seem to find an answer to this, say I have this:
setInterval(function() {
m = Math.floor(Math.random()*7);
$('.foo:nth-of-type('+m+')').fadeIn(300);
}, 300);
How do I make it so that random number doesn't repeat itself. For example if the random number is 2, I don't want 2 to come out again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有多种方法可以实现这一目标。
方案一:
如果数字范围不大(假设小于 10),您可以只跟踪已经生成的数字。然后,如果生成重复的数字,请将其丢弃并生成另一个数字。
方案B:
预先生成随机数,将它们存储到数组中,然后遍历该数组。您可以通过获取数字
1,2,...,n
然后将它们打乱来实现此目的。方案C:
跟踪数组中可用的数字。随机选择一个数字。从所述数组中删除数字。
There are a number of ways you could achieve this.
Solution A:
If the range of numbers isn't large (let's say less than 10), you could just keep track of the numbers you've already generated. Then if you generate a duplicate, discard it and generate another number.
Solution B:
Pre-generate the random numbers, store them into an array and then go through the array. You could accomplish this by taking the numbers
1,2,...,n
and then shuffle them.Solution C:
Keep track of the numbers available in an array. Randomly pick a number. Remove number from said array.
您似乎想要一个从 0 到 6 的不重复随机数,与 tskuzzy 的答案类似:
它将按随机顺序返回数字 0 到 6。当每个都被绘制一次后,它将重新开始。
You seem to want a non-repeating random number from 0 to 6, so similar to tskuzzy's answer:
It will return the numbers 0 to 6 in random order. When each has been drawn once, it will start again.
你能尝试一下吗
could you try that,
我喜欢尼尔的回答,尽管这是需要一些递归。这是用java编写的,你仍然会得到大概的想法。请注意,如果您提取的数字多于 MAX,您将遇到无限循环,我可以修复该问题,但为了清楚起见,将其保留原样。
编辑:看到尼尔添加了一个 while 循环,这样效果很好。
I like Neal's answer although this is begging for some recursion. Here it is in java, you'll still get the general idea. Note that you'll hit an infinite loop if you pull out more numbers than MAX, I could have fixed that but left it as is for clarity.
edit: saw neal added a while loop so that works great.
一般来说,我的方法是创建一个包含所有可能值的数组,并执行以下操作:
结果集数字将包含您的所有索引而不重复。
更好的是,也许是这样的:
然后只需浏览这些项目,因为随机播放会将它们随机化,并一次将它们弹出一个。
Generally my approach is to make an array containing all of the possible values and to:
The resulting set of numbers will contain all of your indices without repetition.
Even better, maybe something like this:
Then just go through the items because shuffle will have randomized them and pop them off one at a time.
这是一个简单的修复,虽然有点初级:
如果下一个数字与上一个数字相同,只需减 1,除非该数字为 0(零)并将其设置为集合中的任何其他数字(我选择 7,最高索引) 。
我在循环函数中使用了这种方法,因为选择数字的唯一规定是不能与上一个数字相同。
这不是最优雅或技术最精湛的解决方案,但它确实有效:)
Here's a simple fix, if a little rudimentary:
If the next number is the same as the last simply minus 1 unless the number is 0 (zero) and set it to any other number within your set (I chose 7, the highest index).
I used this method within the cycle function because the only stipulation on selecting a number was that is musn't be the same as the last one.
Not the most elegant or technically gifted solution, but it works :)
使用套装。它们在 ES6 中被引入到规范中。集合是一种表示唯一值集合的数据结构,因此它不能包含任何重复值。我需要 6 个随机的、不可重复的数字,范围从 1 到 49。我首先创建一个大约 30 位的较长集合(如果值重复,该集合将包含更少的元素),将集合转换为数组,然后将其前 6 个元素切片。简单易行。 Set.length 默认情况下未定义,它是无用的,这就是为什么如果您需要特定长度,将其转换为数组会更容易。
Use sets. They were introduced to the specification in ES6. A set is a data structure that represents a collection of unique values, so it cannot include any duplicate values. I needed 6 random, non-repeatable numbers ranging from 1-49. I started with creating a longer set with around 30 digits (if the values repeat the set will have less elements), converted the set to array and then sliced it's first 6 elements. Easy peasy. Set.length is by default undefined and it's useless that's why it's easier to convert it to an array if you need specific length.
生成不同数字列表的简单方法,无论大小或数量如何:
An easy way to generate a list of different numbers, no matter the size or number:
我想补充一点——
这使用时间戳(每毫秒)来始终生成一个唯一的数字。
I would like to add--
This uses timestamp (every millisecond) to always generate a unique number.
最小到最大之间的随机数
Random Number Between Min to Max
你可以做到这一点。拥有您使用过的密钥的公共数组,并使用此函数检查它们:(
函数来自: javascript function inArray< /a>)
所以你可以做的是:
这段代码在获取所有七个数字后会卡住,所以你需要确保它在找到所有数字后存在。
you can do this. Have a public array of keys that you have used and check against them with this function:
(function from: javascript function inArray)
So what you can do is:
This code will get stuck after getting all seven numbers so you need to make sure it exists after it fins them all.