Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C# program to generate unique random integers. The program will ask the

ID: 3852080 • Letter: W

Question

Write a C# program to generate unique random integers. The program will ask the user three things:

(a) number of random integers the user wants to generate

(b) the lower limit of the random integers (i.e. how small the random integers can be)

(c) the upper limit of the random integers (i.e. how large the random integers can be)

Display the random numbers generated. These numbers are all unique (i.e. No two numbers are the same). The following is an example:

If the numbers of random integers the user want to generate is greater than the number of possible unique integers between the specified limits, display an error message and ask the user to re-enter his request:

In the example above, the user asks for 10 unique random integers in the range of 0 and 5. However, there are only 6 unique integers in this range: 0, 1, 2, 3, 4 and 5. Therefore, it is impossible for the program to satisfy the user’s request.

The user re-enter his request. The program generates a list of random integers the user asked for:

C:Windows system32cmd.exe How many random number do you want to generate? 10 ow small can these random integers be? How large can these random integers be? 7 12 14 9 11 8 7 10 16 6 Press any key to continue . . -

Explanation / Answer

Below is your program: -

using System;
using System.Collections;
public class Test
{
   public static void Main()
   {
       bool flag = true;
while (flag) {
           Console.WriteLine("How many random numbers do you want to generate?");
  
           int totalNums = Convert.ToInt32(Console.ReadLine());
  
           Console.WriteLine("How small can these random integers be?");
           int min = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("How large can these random integers be?");
           int max = Convert.ToInt32(Console.ReadLine());
           if(totalNums > max) {
               Console.WriteLine("Error: Number of random integers requested is more than the number of unique integers in the range.");  
           } else {
               Random randoms = new Random();
ArrayList lists = new ArrayList();
int n;
int count = 0;
while(count != totalNums) {
n = randoms.Next(min, max) + 1;
if(!lists.Contains(n)) {
lists.Add(n);
count++;
}
}
               foreach (int i in lists)
{
Console.Write(i + " ");
}
               flag = false;
           }
}
   }
}

Sample run: -

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote