-2

Part 1: All I am wanting to achieve is to write the numbers 1, 2, 3 ... 8, 9, 10 to the console window in random order. So all the numbers will need to be written to console window, but the order of them must be random.

Part 2: In my actual project I plan to write all of the elements in an array, to the console window in random order. I am assuming that if I can get the answer to part 1, I should easily be able to implement this with an array.

4 Answers4

0

Enumerable.Range(1, 10).OrderBy(i => Guid.NewGuid()) works nicely.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
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;
        }
    }

Main

  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;
        }
    }
}
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
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);
    }
}

In your program

Console.WriteLine("All numbers between 0 and 10 in random order:");
foreach (int i in UniqueRandom(0, 10)) {
    Console.WriteLine(i);
}
Matheno
  • 4,112
  • 6
  • 36
  • 53
-2
int[] ints = new int[11];
Random rand = new Random();

Random is a class built into .NET, and allows us to create random integers really, really easily. Basically all we have to do is call a method inside our rand object to get that random number, which is nice. So, inside our loop, we just set each element to the results of that method:

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

We are essentially filling our entire array with random numbers here, all between 0 and 10. At this point all we have to do is display the contents for the user, which can be done with a foreach loop:

foreach (int i in ints)
{
  Console.WriteLine(i.ToString());
}
Matheno
  • 4,112
  • 6
  • 36
  • 53
  • OP doesn't want duplicate numbers, so this is wrong. Also this gives numbers in the range 0..9 and the OP wants 1..10 – Matthew Watson Jun 20 '13 at 09:49
  • This was just a rough example, OP can change the range of max numbers himself I hope. ;) – Matheno Jun 20 '13 at 09:53
  • Yes but the more fundamental problem is that your code can produce duplicate numbers, and miss other numbers out altogether. The OP wants the numbers 1..10 arranged in a random order, not a set of random numbers each of which is in the range 1..10. (And note that your recent edit now produces random numbers in the range 0..10, not 1..10 anyway) – Matthew Watson Jun 20 '13 at 09:56
  • I added a new solution, which works for me. – Matheno Jun 20 '13 at 09:59
  • 1
    Aye, that new answer will work. – Matthew Watson Jun 20 '13 at 10:20