我在Xamarin新手,生成4个型动物数量随机
我想随机产生4个数字,其在列表中,这4个数字必须是不同的。
在下面的例子中,我有一个ID列表,我试图在列表中随机选择4个ID,这4个ID必须是每个不同的。
这里是我的梅索德,我不知道怎样才能继续使之简单:
public MyWordsList()
{
InitializeComponent();
Dictionary<int, int> WordId = new Dictionary<int, int>();
int u= 0;
// TestAnswer.IsVisible = false;
foreach (var w in mywords)
{
WordId[u] = w.ID;
u++;
}
Random rnd = new Random();
// this is not ok because I can have the same number
word11.Text = WordsList[rnd.Next(1, 20)];
word12.Text = WordsList[rnd.Next(1, 20)];
word13.Text = WordsList[rnd.Next(1, 20)];
word14.Text = WordsList[rnd.Next(1, 20)];
}
如果你有一个更好的解决方案,我会抓住。 感谢
HTTPS:/ /stackoverflow.com/a/43592646/1744164 –
可能的重复[如何随机用3个不同的字符串填充数组?](https://stackoverflow.com/questions/43592102/how-can-i-randomly-fill -an-array-with-3-different-strings) –