可能是重复问题:
如何访问列表中的随机元素
我有一个包含数字的数组,想要从中获取随机元素。例如:{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
的洗牌副本吗? - H Hcaminohormiga
数组进行洗牌(shuffle)还是旋转(rotate)操作? - Alexander Balte