starbeamrainbowlabs

Coding Conundrums 1.2: Number Shuffle

Feb 9th, 2015
216
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class Program
  4. {
  5.     public static void PrintHelp()
  6.     {
  7.         Console.WriteLine("Coding Conundrums Problem 1.2 Solution: Number Shuffle");
  8.         Console.WriteLine("------------------------------------------------------");
  9.         Console.WriteLine("Problem Description:");
  10.         Console.WriteLine("    I want a program that will print outthe numbers 1,2,3,4,5,6,7,8,9 in a");
  11.         Console.WriteLine("    shuffled order. The order must be different each time the program runs. Note");
  12.         Console.WriteLine("    that the same number must be different each time. It should be possible to");
  13.         Console.WriteLine("    extend this to work with 52 numbers, in which case I can make a shuffled");
  14.         Console.WriteLine("    deck of cards.");
  15.         Console.WriteLine();
  16.         Console.WriteLine("Use it like this: ");
  17.         Console.WriteLine("    numbershuffle.exe <count-to>");
  18.         Console.WriteLine();
  19.         Console.WriteLine("Paramters:");
  20.         Console.WriteLine("    <count-to>: The number to count up to when generating the initial sequence of numbers.");
  21.     }
  22.  
  23.     public static void Main(string[] args)
  24.     {
  25.         if(args.Length != 1)
  26.         {
  27.             PrintHelp();
  28.             return;
  29.         }
  30.  
  31.         int numberCount;
  32.         if(!int.TryParse(args[0], out numberCount))
  33.         {
  34.             PrintHelp();
  35.             return;
  36.         }
  37.  
  38.  
  39.  
  40.         int[] numbers = new int[numberCount];
  41.         for(int i = 0; i < numberCount; i++)
  42.         {
  43.             numbers[i] = i + 1;
  44.         }
  45.  
  46.         int swapsCount = numberCount * 2;
  47.         int temp, a, b;
  48.         Random random = new Random();
  49.         for(int i = 0; i < swapsCount; i++)
  50.         {
  51.             a = random.Next(numbers.Length);
  52.             b = random.Next(numbers.Length);
  53.  
  54.             temp = numbers[a];
  55.             numbers[a] = numbers[b];
  56.             numbers[b] = temp;
  57.         }
  58.  
  59.         for(int i = 0; i < numbers.Length; i++)
  60.         {
  61.             Console.Write("{0} ", numbers[i]);
  62.         }
  63.     }
  64. }
RAW Paste Data