C# 中多维数组的随机化
对于一个迷你项目,我正在制作一个测验程序 我当前的(相关)代码如下:
static Random _r = new Random();
static int Quiz()
{
string[,] QAndA = {
{"What is the capital of France", "Paris"},
{"What is the capital of Spain", "Madrid"},
...
{"What is the captial of Russia", "Moscow"},
{"What is the capital of Ukraine", "Kiev"},
};
for (int i = 0; i < NUM_QUESTIONS; i++)
{
int num = _r.Next(QAndA.GetLength(0) / 2);
Question(QAndA[num, 0], QAndA[num, 1]);
}
}
现在,明显的问题是随机数可以重复,这意味着问题可以重复。
现在,我的老师(是的,这是学校的事情)告诉我寻找洗牌算法,但我未能找到任何适用于我使用的多维数组的算法。
我是一个相当新的 C# 程序员,但我有 C++ 经验 并且该程序是一个命令行程序(目前:)),如果这很重要/有帮助
那么,问题是,将多维数组重新排序/洗牌为随机顺序的最佳方法是什么?
For a mini project I am making a quiz program
My current (relavant) code is as follows:
static Random _r = new Random();
static int Quiz()
{
string[,] QAndA = {
{"What is the capital of France", "Paris"},
{"What is the capital of Spain", "Madrid"},
...
{"What is the captial of Russia", "Moscow"},
{"What is the capital of Ukraine", "Kiev"},
};
for (int i = 0; i < NUM_QUESTIONS; i++)
{
int num = _r.Next(QAndA.GetLength(0) / 2);
Question(QAndA[num, 0], QAndA[num, 1]);
}
}
Now, the obvious problem with this is that the random numbers can be repeated, meaning that questions can be repeated.
Now, my teacher (yes, this is a school thing) told me to look for shuffling algorithms, but I have failed to find any that work for multidimensional arrays like i have used.
I am a fairly new c# programmer, but I have experience with c++
and the program is a commandline program (at the moment :) ), if that matters/helps
So, the question is, what's the best way of reordering/shuffling the multidimensional array to be in a random order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你正在寻找错误的问题。使用锯齿状数组代替多维数组(很少使用,因为几乎不支持)。
或者(更好)创建一个包含两个成员的类:
Question
和Answer
。然后,您可以使用 Knuth 算法:-)
引用那里
: C# 算法类似于
You are looking at the wrong problem. Instead of a multidimensional array (something quite rarely used because scarcely supported) use a jagged array.
or (better) create a class with two members,
Question
andAnswer
.You could then use the Knuth algorithm :-)
Quoting from there:
In C# the algorithm will be something like
如果它不是多维数组,我建议不使用“多维”数组。
我的建议:(查看现场http://ideone.com/NsjfM)
I suggest NOT using a 'multidimensional' array if it is ... not an multidemensional array.
My suggestion: (see it live here http://ideone.com/NsjfM)
实际上,您正在重新排序一个维数数组,因为您不应该打乱答案;)最简单的算法可能是:
actually you're reordeing one dimential array, because you shouldn't shufftle answers ;) simplest algorithm may be:
一种不需要您重新排列数组并且只需选择几个问题的方法会更快,即将您选择的问题存储在一个集合中。
继续生成随机数并将该索引处的问题添加到集合中,一旦集合的大小正确就将其返回。
您的循环将类似于:
HashSet<>.Count
和HashSet<>.Add()
都是 O(1),因此限制因素将是多少随机数发生碰撞。A method that doesn't require you to reshuffle the array and which will be quicker if you only need to pick a few questions is to store your selected questions in a set.
Keep generating random numbers and adding the question at that index to the set, once the set is of the correct size return it.
Your loop will look something like:
HashSet<>.Count
andHashSet<>.Add()
are both O(1) so the limiting factor will be how many random numbers collide.