如何从数组在C#中的随机值 [英] How to get random values from array in C#
问题描述
可能重复:结果
在列表 访问随机项
块引用>我有一个数字数组,我想从这个数组随机元素。例如:{0,1,4,6,8,2}。我想选择6,并把在另一个阵列这个号码,以及新的阵列将具有值{6,....}。
我使用random.next(0,array.length),但这给出的长度的随机数和我所需要的随机阵列的数字。
的for(int i = 0; I< caminohormiga.Length;我++)
{
如果(caminohormiga [I] == 0)
{
继续;
} 对于(INT J = 0; J< caminohormiga.Length; J ++)
{
如果(caminohormiga [J] == caminohormiga [1] - 放大器;&安培;!I = j)条
{
caminohormiga [J] = 0;
}
}
}的for(int i = 0; I< caminohormiga.Length;我++)
{
INT START2 = random.Next(0,caminohormiga.Length);
Console.Write(START2);
}返回caminohormiga;
解决方案
我使用random.next(0,array.length),但是这给长度的随机数和我需要的随机阵列的数字。
块引用>使用从
random.next(0,array.length)
为指标,从获得价值阵列$ C $的返回值C>
INT START2 = random.Next(0,caminohormiga.Length);
Console.Write(caminohormiga [START2]);Possible Duplicate:
Access random item in listI 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;
解决方案I use the random.next(0, array.length), but this give random number of the length and i need the random array numbers.
Use the return value from
random.next(0, array.length)
as index to get value from thearray
int start2 = random.Next(0, caminohormiga.Length); Console.Write(caminohormiga[start2]);
这篇关于如何从数组在C#中的随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!