非重复随机数生成器?
我使用 Visual Basic for applications (Excel) 创建了一个问答游戏,该游戏通过浏览案例陈述来选择问题,其中案例是数字。我让程序从 1 到最大问题数中随机选择一个数字。使用这种方法,游戏会重复问题。
有没有办法制作随机生成数字的东西(每次都有不同的结果)并且不会多次重复一个数字?在完成所有数字后,它需要执行特定的代码。 (我将输入结束游戏并显示他们答对和错的问题数量的代码)
我想到了几种不同的方法来做到这一点,但是我什至无法开始考虑语法可能是什么。
I created a trivia game using visual basic for applications (Excel) that chooses questions by going through a case statement where the cases are numbers. I have the program randomly select a number from 1 to the max amount of questions there are. Using this method, the game repeats questions.
Is there a way to make something that generates numbers randomly (different results every time) and doesn't repeat a number more than once? And after it's gone through all the numbers it needs to execute a certain code. (I'll put in code that ends the game and displays the number of questions they got right and got wrong)
I thought of a few different ways to do this, however I couldn't even begin to think of what the syntax might be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来你需要一个数组洗牌器!
查看下面的链接 -
http://www.cpearson.com/excel/ShuffleArray.aspx
Sounds like you need an Array Shuffler!
Check out the below link -
http://www.cpearson.com/excel/ShuffleArray.aspx
这是另一种看法。它生成一个唯一的随机长数组。
在此示例中,我使用 1 到 100。它通过使用集合对象来实现此目的。然后,您可以对 qArray 中的每个数组元素进行正常循环,而无需多次随机化。
Here's yet another take. It generates an array of unique, random longs.
In this example, I use 1 to 100. It does this by using the collection object. Then you can just do a normal loop through each array element in qArray without the need to randomize more than once.
我看到你有答案了,我正在研究这个问题,但失去了互联网连接。无论如何,这是另一种方法。
I see you have an answer, I was working on this but lost my internet connection. Anyway here is another method.
不管它的价值如何,我对这个问题的尝试都是如此。该函数使用布尔函数而不是数值数组。它非常简单但非常快。它的优点(我并不是说它是完美的)是对长范围内的数字的有效解决方案,因为您只检查已经选择并保存的数字,并且不需要潜在的大数组来保存值您已拒绝,因此不会因数组的大小而导致内存问题。
For whatever it's worth here is my stab at this question. This one uses a boolean function instead of numerical arrays. It's very simple yet very fast. The advantage of it, which I'm not saying is perfect, is an effective solution for numbers in a long range because you only ever check the numbers you have already picked and saved and don't need a potentially large array to hold the values you have rejected so it won't cause memory problems because of the size of the array.