Advertisement
Guest User

RandomSequence

a guest
May 27th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication3
  8. {
  9.     class RandomizeNums1toN
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter n: ");
  14.             int n = int.Parse(Console.ReadLine());
  15.             List<int> numbers = new List<int>();        
  16.             Random rand = new Random();
  17.  
  18.             for (int i = 0; i < n; i++)
  19.             {
  20.                 int num = rand.Next(0, n) + 1;
  21.  
  22.                 while (numbers.Contains(num))
  23.                 {
  24.                     num = rand.Next(0, n) + 1;
  25.                 }
  26.                 numbers.Add(num);
  27.             }
  28.             for (int i = 0; i < n; i++)
  29.             {
  30.                 Console.Write(numbers[i] + " ");
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement