我有一个数字数组,我想从这个数组随机元素。例如:{0,1,4,6,8,2}。我想选择6并将该数字放在另一个数组中,新数组的值将为{6,....}。
我使用random.next(0,array.length),但是这给出了一个随机数的长度,我需要随机数组数。
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;
用一个简单的随机选择,你会得到重复。你想返回一个洗牌的'caminohormiga'吗? –
@shebystian是否重复适用? – exexzian
你想洗牌或旋转你的'caminohormiga'阵列吗? –