关于RNG的简单问题 [英] Simple Question About RNG
本文介绍了关于RNG的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天我有一个问题。
假设我有随机类中的10个数字(1-10)
我打印一个随机数10次。输出会例如 -
5
7
9
1 br />
10
1
4
3
10
2
您可以看到有一些数字已被打印过多次。
我想打印所有数字而不重复它们。
例如。
number1 =(1-10)
number2 =(1-10-不是数字1)
number3 =(1-10-不是数字1或数字2)
等等。
我该怎么做?
I have a question today.
Suppose I have 10 numbers in random class(1-10)
and I print a random number 10 times.The output would be For example-
5
7
9
1
10
1
4
3
10
2
You can see there are some numbers that have been printed more than once.
I want to print all numbers without repeating them.
for eg.
number1=(1-10)
number2=(1-10-not number1)
number3=(1-10-not number1 or number2)
and so on.
How do I do this?
推荐答案
首先,真正的随机性并不能保证序列中永远不会出现两个具有相同值的数字。
例如,掷骰子被认为是随机的,但如果你连续得到三个六,你就不会感到震惊。
那就是说,问题的解决方案是将你的号码存储在一个列表中,生成一个新号码并检查它是否存在于列表中。
如果是,请生成一个新号码并再次检查。
First of all, true randomness does not guarantee that two numbers with the same value never occur in a sequence.
For example, throwing a die is considered to be random, but you would not be shocked if you get three sixes in a row, right.
That said, the solution to your problem is to store your numbers in a list, generate a new number and check if it exists in the list.
If it does, generate a new number and check again.
List<int> randomNumbers = new List<int>();
Random randy = new Random();
while (randomNumbers.Count < 10)
{
int randomNumber = randy.Next(1,10);
if (!randomNumbers.Contains(randomNumber))
randomNumbers.Add(randomNumber);
}
算法在我的提示中:从牌组中随机抽取5张牌 [ ^ ]。
The algorithm is in my tip: "Random extraction of 5 cards from a deck"[^].
这篇关于关于RNG的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文