Home > Software engineering >  how to pick a value in order from 1 to 10
how to pick a value in order from 1 to 10

Time:11-13

am working with C# and Selenium and I need to send keys into a text field it would need to pick a name (the names I provide into a textbox) and then pick then send the first one (with send keys) after its done with the first one it would move to the second and so on. I have done this so far (but this would randomly pick a name and not in a order way from first to last)

var random = new Random();

//this is the textbox in which I place the "names"\\
        var Names = question1.Text.Split();

        int index = random.Next(Names.Length);

        string randomName = Names[index];`

this would just pick a random value I place into a textbox so I will need that but the issue with this is if am sending lets say 100 names after 25/50 the names would just start to be duplicates which led to an error so thats why I need it to select the names in order.

CodePudding user response:

I think you can add all the names in Hashset and then using for loop you can pick names one by one

CodePudding user response:

It looks like you need to use the shuffle algorithm first and then create a queue of shuffled names:

var shuffledNames = question1.Text.Split().OrderBy(_ => Guid.NewGuid());
var queueOfShuffledNames = new Queue<string>(shuffledNames);
        
Console.WriteLine($"Queue of shuffled names: {string.Join(", ", queueOfShuffledNames)}");
Console.WriteLine($"Selected name: {queueOfShuffledNames.Dequeue()}");
Console.WriteLine($"Queue of shuffled names: {string.Join(", ", queueOfShuffledNames)}");

Link: https://dotnetfiddle.net/9Hfkbt

If you need to remove duplication from the original collection and limit the number of items in the queue, create the shuffledNames this way:

var shuffledNames = question1.Text.Split()
    .OrderBy(_ => Guid.NewGuid())
    .Distinct()
    .Take(10);
  • Related