如何从C#中的数组中获取随机值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14297853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:21:19  来源:igfitidea点击:

How to get random values from array in C#

c#.netarraysrandomcollections

提问by Shebystian

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;

采纳答案by Tilak

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)作为索引的返回值从array

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

回答by faester

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)];

回答by SergeyS

Console.Write(caminohormiga[start2]);

回答by Murshid Ahmed

Try like this

像这样尝试

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

instead of

代替

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

回答by Seth Flowers

To shuffle

洗牌

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

回答by Erik

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 randomand pushthem 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());
}