I have tried posting before, but after editing the question 5 times it got cloudy and did not help anyone. So I will ask properly this time so please consider the help (If you believe this is a duplicate, then do so for the older post that is only making things worse)
Firstly I want to make a very simple program using C# that will randomly display all the numbers that qualify for ALL the following criteria:
1.Number generated must be between 1123 and 5543 inclusive
2.Number generated must only have digits 1,2,3,4 and 5
3.Number generated must not be repeated
4.Each number must have one of its digits repeated ( as in 1146 or 2627) not more
When I did some maths, I came up with the conclusion that there are exactly 300 numbers that fit here.
The following code does part of the job: It Produces a 4 digit number with only the digits 1-5. But there are 3 other rules needed that I don't know how to enforce. It is my first time using C#, (only VB.Net in the past) so I have no idea how to go further.
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)
Just the thing about Console.WriteLine, I don't really care how the output is displayed. I saw a suggestion stating I export the data into a .txt file, but all I am concerned with is seeing the numbers all at once, but separated (by breaks or commas or double spacing or anything) from each other.
What I mean by "randomly" at the beginning is not completely random; I mean random out of the 300 possibilities until all are finished.
It does not concern me whether they are in order or not, I am allowed to use some online service to order them once I'm done to make things easier.
Thanks in advance, and just a quick note, this is not a homework question because "allowed" and "required". The backstory is irrelevant to Stack Overflow so I omitted it.
Like I said, if you think this is a duplicate, it is not. The other post is gone too messy.
Aucun commentaire:
Enregistrer un commentaire