本文介绍了如何从数组在C#中的随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have an array with numbers and I want to get random elements from this array. For example: {0,1,4,6,8,2}. I want to select 6 and put this number in another array, and the new array will have the value {6,....}.
I use random.next(0, array.length), but this gives a random number of the length and I need the random array numbers.
for (int i = 0; i < caminohormiga.Length; i++ )
{
if (caminohormiga[i] == 0)
{
continue;
}
for (int j = 0; j < caminohormiga.Length; j++)
{
if (caminohormiga[j] == caminohormiga[i] && i != j)
{
caminohormiga[j] = 0;
}
}
}
for (int i = 0; i < caminohormiga.Length; i++)
{
int start2 = random.Next(0, caminohormiga.Length);
Console.Write(start2);
}
return caminohormiga;
解决方案
Use the return value from random.next(0, array.length)
as index to get value from the array
int start2 = random.Next(0, caminohormiga.Length);
Console.Write(caminohormiga[start2]);
这篇关于如何从数组在C#中的随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!