Advertisement
Masovski

[C# Basics][Loops-HW] 12.* Randomize the Numbers 1…N

Mar 29th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class RandomizeTheNumbers1ToN
  5. {
  6.     static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.         int[] nArray;
  10.         List<int> uniquePos = new List<int>();
  11.  
  12.         nArray = new int[n];
  13.         Random randomGenerator = new Random();
  14.  
  15.         for (int i = 0; i < nArray.Length; i++)
  16.         {
  17.             int randomIndex = randomGenerator.Next(nArray.GetLowerBound(0), nArray.GetUpperBound(0)+1);
  18.            
  19.             while (uniquePos.Contains(randomIndex)) //checking if the number is already printed
  20.             {
  21.                 randomIndex = randomGenerator.Next(nArray.GetLowerBound(0), nArray.GetUpperBound(0)+1);
  22.             }
  23.  
  24.             nArray[i] = randomIndex + 1;
  25.             uniquePos.Add(randomIndex);
  26.             Console.WriteLine(nArray[i]);
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement