samedi 11 juillet 2020

Generating non repeating strings in c#

Currently I am writing a console based program in .net Core 3.1. The goal of the program is to have the user input a string and then the console will attempt to guess it. Example:

What shall I guess: string
Ok Guessing String...

Code Cracked :) Your code is: string

Anyways while I was creating the program I ran into a problem. The randomly generated string I created was repeating quite frequently, and if the codes were longer the problem exponentially increased.

I used this for my random string generation

string passw = Console.Readline();

for (int i = 0; i > -1; i++)
            {
                var chars = "abcdefghijklmnopqrstuvwxyz";
                var stringChars = new char[passw.Length];

                for (int a = 0; a < passw.Length; a++)
                {
                    stringChars[a] = chars[rnd.Next(chars.Length)];
                }

                var finalstring = new String(stringChars);
                Console.WriteLine(finalstring);

                if (finalstring== passw)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Password Guessed: " + finalstring);
                    Console.WriteLine("It took a total of: " + i + " guesses!");
                    Environment.Exit(0);
                }
            }

How can I accomplish the goal? I saw some things about lists and similar things but was wondering if there was a better way to get the result I am looking for. Thank you! I can clarify anything if needed!




Aucun commentaire:

Enregistrer un commentaire