dimanche 17 mars 2019

Randomize string in C#

I am trying to randomize string elements, but my code is repeating strings. Can someone explain what is wrong with my code?

string[] words = Console.ReadLine().Split();
//input = "Welcome and have fun learning programming"

Random number = new Random();

for (int i = 0; i < words.Length; i++)
{
    int currRandomNumber = number.Next(0, words.Length);
    words[i] = words[currRandomNumber];
}

Console.WriteLine(string.Join(' ', words));      
//output = "have learning learning learning learning programming"

I am facing problems with the words repeating, and it is not randomized? If you don't understand what I mean, see the comments which I added in the code. Any help will be appreciated!




Aucun commentaire:

Enregistrer un commentaire