C# - 以随机顺序写入数字1到10

时间:2013-06-20 09:43:16

标签: c# random console-application

第1部分:我想要实现的是将数字1,2,3 ... 8,9,10以随机顺序写入控制台窗口。因此,所有数字都需要写入控制台窗口,但它们的顺序必须是随机的。

第2部分:在我的实际项目中,我计划以随机顺序将数组中的所有元素写入控制台窗口。我假设如果我能得到第1部分的答案,我应该能够轻松地用数组来实现它。

4 个答案:

答案 0 :(得分:0)

Enumerable.Range(1, 10).OrderBy(i => Guid.NewGuid())效果很好。

答案 1 :(得分:0)

using System;
using System.Collections;

namespace ConsoleApplication
{
    class Numbers
    {
        public ArrayList RandomNumbers(int max)
        {
            // Create an ArrayList object that will hold the numbers
            ArrayList lstNumbers = new ArrayList();
            // The Random class will be used to generate numbers
            Random rndNumber = new Random();

            // Generate a random number between 1 and the Max
            int number = rndNumber.Next(1, max + 1);
            // Add this first random number to the list
            lstNumbers.Add(number);
            // Set a count of numbers to 0 to start
            int count = 0;

            do // Repeatedly...
            {
                // ... generate a random number between 1 and the Max
                number = rndNumber.Next(1, max + 1);

                // If the newly generated number in not yet in the list...
                if (!lstNumbers.Contains(number))
                {
                    // ... add it
                    lstNumbers.Add(number);
                }

                // Increase the count
                count++;
            } while (count <= 10 * max); // Do that again

            // Once the list is built, return it
            return lstNumbers;
        }
    }

主要

  class Program
    {
        static int Main()
        {
            Numbers nbs = new Numbers();
            const int Total = 10;
            ArrayList lstNumbers = nbs.RandomNumbers(Total);

            for (int i = 0; i < lstNumbers.Count; i++)
                Console.WriteLine("{0}", lstNumbers[i].ToString());

            return 0;
        }
    }
}

答案 2 :(得分:0)

/// <summary>
/// Returns all numbers, between min and max inclusive, once in a random sequence.
/// </summary>
IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
    List<int> candidates = new List<int>();
    for (int i = minInclusive; i <= maxInclusive; i++)
    {
        candidates.Add(i);
    }
    Random rnd = new Random();
    while (candidates.Count > 0)
    {
        int index = rnd.Next(candidates.Count);
        yield return candidates[index];
        candidates.RemoveAt(index);
    }
}

在你的程序中

Console.WriteLine("All numbers between 0 and 10 in random order:");
foreach (int i in UniqueRandom(0, 10)) {
    Console.WriteLine(i);
}

答案 3 :(得分:-2)

int[] ints = new int[11];
Random rand = new Random();

Random是一个内置于.NET的类,它允许我们真正,非常容易地创建随机整数。基本上我们所要做的就是调用rand对象中的方法来获取随机数,这很好。因此,在我们的循环中,我们只需将每个元素设置为该方法的结果:

for (int i = 0; i < ints.Length; i++)
{
  ints[i] = rand.Next(11);
}

我们基本上在这里用随机数填充整个数组,所有这些都在0到10之间。此时我们要做的就是显示用户的内容,这可以通过foreach循环来完成:

foreach (int i in ints)
{
  Console.WriteLine(i.ToString());
}