【发布时间】:2012-12-27 04:44:18
【问题描述】:
我有一个带有数字的数组,我想从这个数组中获取随机元素。例如:{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 是否适用重复项??
-
您要随机播放还是旋转您的
caminohormiga数组? -
如果您不想要重复并假设输出,最好使用 shuffle 而不是检查重复,这需要时间
标签: c# .net arrays random collections