vendredi 12 janvier 2018

Random Integer, no repeat, within specific range

Detailed scenario:

There are 300 possible numbers that fit into the discription.

That is, the number must between 1123 and 5543 inclusive

The number is unique and not repeated

The number only has the integers 1,2,3,4 and 5.

What I am trying to achieve is a program that can display all those numbers at once in ascending order.

My current code:

var chars = "12345";
var stringChars = new char[4];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
Console.WriteLine(finalString)

This code works fine, but I have 2 additional requirements.

  1. Loop 300 times
  2. Display all results in ascending order

EDIT:

I tried using a goto statement to let the program repeat until there are no possible combinations, but it didn't work; I would then start getting the error "The name 'chars' does not exist in the current context."

Please help




Aucun commentaire:

Enregistrer un commentaire