如何随机化数组中的数字

发布于 2024-11-04 11:55:08 字数 134 浏览 1 评论 0原文

我有一个像这样的数组:

int[] numbers = new [] { 1, 2, 3, 4 };

我想随机化它(每次都不同),以便它生成另一个具有相同大小和数字但每次顺序不同的数组。

I have an array like this one:

int[] numbers = new [] { 1, 2, 3, 4 };

I'd like to randomize this (different each time) so that it makes another array with the same size and numbers but in a different order each time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹂绝世的画 2024-11-11 11:55:08

尝试这样的事情:

System.Random rnd = new System.Random();
var numbers = Enumerable.Range(1, 4).OrderBy(r => rnd.Next()).ToArray();

Try something like this:

System.Random rnd = new System.Random();
var numbers = Enumerable.Range(1, 4).OrderBy(r => rnd.Next()).ToArray();
森末i 2024-11-11 11:55:08

下面是一个可行的方法:

public List<int> Randomize(int[] numbers)
{
    List<int> randomized = new List<int>();
    List<int> original = new List<int>(numbers);
    Random r = new Random();
    while (original.Count > 0) {
        int index = r.Next(original.Count);
        randomized.Add(original[index]);
        original.RemoveAt(index);
    }

    return randomized;
}

编辑:

另一种方法可以是对 IEnumerable 集合使用 LINQ 扩展方法:

var random = new Random();
List<int> randomized = numbers.OrderBy(x => random.Next()).ToList();

如果您想要一个数组而不是 List 您可以调用 .ToArray() 来代替。

当然,这适用于任何 int 数组,而不仅仅是 1, 2, 3, ..., n。您甚至可以在 T 中使该方法通用。

Here's a method that will work:

public List<int> Randomize(int[] numbers)
{
    List<int> randomized = new List<int>();
    List<int> original = new List<int>(numbers);
    Random r = new Random();
    while (original.Count > 0) {
        int index = r.Next(original.Count);
        randomized.Add(original[index]);
        original.RemoveAt(index);
    }

    return randomized;
}

Edit:

Another way could be using LINQ extension methods for IEnumerable<T> collections:

var random = new Random();
List<int> randomized = numbers.OrderBy(x => random.Next()).ToList();

If you want to have an array instead of a List<int> you can invoke .ToArray() instead.

Of course, that will work for any array of int, not only for 1, 2, 3, ..., n. You can even make the method generic in T.

面犯桃花 2024-11-11 11:55:08
public  static void Shuffle<T>(T[] array)
{
    Random random = new Random();

    for (int i = 0; i < 10; i++)
    {
        int idx = random.Next(i, 10);

        //swap elements
        T tmp = array[i];
        array[i] = array[idx];
        array[idx] = tmp;
    }  
}
public  static void Shuffle<T>(T[] array)
{
    Random random = new Random();

    for (int i = 0; i < 10; i++)
    {
        int idx = random.Next(i, 10);

        //swap elements
        T tmp = array[i];
        array[i] = array[idx];
        array[idx] = tmp;
    }  
}
浅沫记忆 2024-11-11 11:55:08

这甚至可以确保值不重复;

for(int i=0;i<4;i++)
{
  int no_repeat = -1;

  int x,j;

  while(no_repeat!=1)
  {
    x=rand()%4;
    for(j=0;j<4;j++)
    {
      if(numbers[j]==x)
        break;
    }

    if(j==4)
      no_repeat=1;


  }

  if(no_repeat)
  numbers[i]=x;
}

This even takes care that the values dont repeat ;

for(int i=0;i<4;i++)
{
  int no_repeat = -1;

  int x,j;

  while(no_repeat!=1)
  {
    x=rand()%4;
    for(j=0;j<4;j++)
    {
      if(numbers[j]==x)
        break;
    }

    if(j==4)
      no_repeat=1;


  }

  if(no_repeat)
  numbers[i]=x;
}
习ぎ惯性依靠 2024-11-11 11:55:08

在我看来,最简单的方法是:

 int[] grid = new int[9];//size of array

        Random randomNumber = new Random();//new random number
        var rowLength = grid.GetLength(0);
        var colLength = grid.GetLength(1);
        for (int row = 0; row < rowLength; row++)
        {

                grid[col] = randomNumber.Next(4)+1;//Fills grid with numbers from
                                                        //1-4
                Console.Write(String.Format("{0}\t", grid[col]));
                //Displays array in console

            Console.WriteLine();
        }

In my eyes the easiest way would be this:

 int[] grid = new int[9];//size of array

        Random randomNumber = new Random();//new random number
        var rowLength = grid.GetLength(0);
        var colLength = grid.GetLength(1);
        for (int row = 0; row < rowLength; row++)
        {

                grid[col] = randomNumber.Next(4)+1;//Fills grid with numbers from
                                                        //1-4
                Console.Write(String.Format("{0}\t", grid[col]));
                //Displays array in console

            Console.WriteLine();
        }
芯好空 2024-11-11 11:55:08

有用:

numbers = numbers.OrderBy(s => Guid.NewGuid()).ToArray();

It works:

numbers = numbers.OrderBy(s => Guid.NewGuid()).ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文