如何从C#中的数组中获取随机值[重复]

时间:2021-03-10 01:38:52

Possible Duplicate:
Access random item in list

可能重复:访问列表中的随机项

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,....}.

我有一个数字数组,我想从这个数组中获取随机元素。例如:{0,1,4,6,8,2}。我想选择6并将此数字放在另一个数组中,新数组的值为{6,....}。

I use random.next(0, array.length), but this gives a random number of the length and I need the random array numbers.

我使用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;

6 个解决方案

#1


17  

I use the random.next(0, array.length), but this give random number of the length and i need the random array numbers.

我使用random.next(0,array.length),但这给出了随机数的长度,我需要随机数组。

Use the return value from random.next(0, array.length) as index to get value from the array

使用random.next(0,array.length)的返回值作为索引从数组中获取值

 Random random = new Random();
 int start2 = random.Next(0, caminohormiga.Length);
 Console.Write(caminohormiga[start2]);

#2


13  

To shuffle

洗牌

int[] numbers = new [] {0, 1, 4, 6, 8, 2};
int[] shuffled = numbers.OrderBy(n => Guid.NewGuid()).ToArray();

#3


2  

Try like this

试试这样吧

int start2 = caminohormiga[ran.Next(0, caminohormiga.Length)];

instead of

代替

int start2 = random.Next(0, caminohormiga.Length);

#4


1  

You just need to use the random number as a reference to the array:

您只需使用随机数作为数组的引用:

var arr1 = new[]{1,2,3,4,5,6}
var rndMember = arr1[random.Next(arr1.Length)];

#5


1  

I noticed in the comments you wanted no repeats, so you want the numbers to be 'shuffled' similar to a deck of cards.

我在评论中注意到你没有重复,所以你希望数字被“洗牌”类似于一副牌。

I would use a List<> for the source items, grab them at random and push them to a Stack<> to create the deck of numbers.

我会使用List <>作为源项,随机抓取它们并将它们推送到Stack <>以创建数字组。

Here is an example:

这是一个例子:

private static Stack<T> CreateShuffledDeck<T>(IEnumerable<T> values)
{
  var rand = new Random();

  var list = new List<T>(values);
  var stack = new Stack<T>();

  while(list.Count > 0)
  {
    // Get the next item at random.
    var index = rand.Next(0, list.Count);
    var item = list[index];

    // Remove the item from the list and push it to the top of the deck.
    list.RemoveAt(index);
    stack.Push(item);
  }

  return stack;
}

So then:

那么:

var numbers = new int[] {0, 1, 4, 6, 8, 2};
var deck = CreateShuffledDeck(numbers);

while(deck.Count > 0)
{
  var number = deck.Pop();
  Console.WriteLine(number.ToString());
}

#6


0  

Console.Write(caminohormiga[start2]);

#1


17  

I use the random.next(0, array.length), but this give random number of the length and i need the random array numbers.

我使用random.next(0,array.length),但这给出了随机数的长度,我需要随机数组。

Use the return value from random.next(0, array.length) as index to get value from the array

使用random.next(0,array.length)的返回值作为索引从数组中获取值

 Random random = new Random();
 int start2 = random.Next(0, caminohormiga.Length);
 Console.Write(caminohormiga[start2]);

#2


13  

To shuffle

洗牌

int[] numbers = new [] {0, 1, 4, 6, 8, 2};
int[] shuffled = numbers.OrderBy(n => Guid.NewGuid()).ToArray();

#3


2  

Try like this

试试这样吧

int start2 = caminohormiga[ran.Next(0, caminohormiga.Length)];

instead of

代替

int start2 = random.Next(0, caminohormiga.Length);

#4


1  

You just need to use the random number as a reference to the array:

您只需使用随机数作为数组的引用:

var arr1 = new[]{1,2,3,4,5,6}
var rndMember = arr1[random.Next(arr1.Length)];

#5


1  

I noticed in the comments you wanted no repeats, so you want the numbers to be 'shuffled' similar to a deck of cards.

我在评论中注意到你没有重复,所以你希望数字被“洗牌”类似于一副牌。

I would use a List<> for the source items, grab them at random and push them to a Stack<> to create the deck of numbers.

我会使用List <>作为源项,随机抓取它们并将它们推送到Stack <>以创建数字组。

Here is an example:

这是一个例子:

private static Stack<T> CreateShuffledDeck<T>(IEnumerable<T> values)
{
  var rand = new Random();

  var list = new List<T>(values);
  var stack = new Stack<T>();

  while(list.Count > 0)
  {
    // Get the next item at random.
    var index = rand.Next(0, list.Count);
    var item = list[index];

    // Remove the item from the list and push it to the top of the deck.
    list.RemoveAt(index);
    stack.Push(item);
  }

  return stack;
}

So then:

那么:

var numbers = new int[] {0, 1, 4, 6, 8, 2};
var deck = CreateShuffledDeck(numbers);

while(deck.Count > 0)
{
  var number = deck.Pop();
  Console.WriteLine(number.ToString());
}

#6


0  

Console.Write(caminohormiga[start2]);