vendredi 28 août 2020

Random.Next does work, but it doesn't return every value (c#)

i've started programming a charcacter randomizer very recently. When you type "hello" into the console, the program will mix these chars and create a random string with H, E, L and O. It should be as long as the word typed into the console, that means, if you're typing in a 5-letter-word, my program will also return a 5-letter string.

Here's my code:

                string readLine = Console.ReadLine();

                int readLineLength = readedLine.Length;

                Random r = new Random();
                char[] letters = readedLine.ToCharArray();
                string randomString = "";

                for (int i = 0; i < readLineLength; i++)
                {
                    randomString += letters[r.Next(0, readLineLength)].ToString();
                }

                Console.WriteLine(randomString);
                Console.WriteLine("Press ENTER to generate a random constellation of letters.");

Random.Next works fine, but if you type in the word "hello", the program will only mix E, L and O but completely ignore the H.

I hope my issue is pretty clear :D

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire