Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array shuffling #56

Open
TonyTroeff opened this issue Feb 27, 2024 · 2 comments
Open

Add array shuffling #56

TonyTroeff opened this issue Feb 27, 2024 · 2 comments
Labels
enhancement New feature or request feature Something that should be implemented good first issue Good for newcomers
Milestone

Comments

@TonyTroeff
Copy link
Member

Describe your idea
I have an array of values. I want to shuffle them so the ordering is changed randomly.

@TonyTroeff TonyTroeff added enhancement New feature or request good first issue Good for newcomers feature Something that should be implemented labels Feb 27, 2024
@TonyTroeff TonyTroeff added this to the 2.0.3 milestone Feb 27, 2024
@Meysamanhari
Copy link

it's my simple idea

public void arrayShuffling()
{
    List<int> numbers = [1, 2, 3,4,5,6,7,8,9,10,13,15,18];
    int numbersCount=numbers.Count;
    for (int i=0; i<numbersCount; i++)
    {
        int randomIndex=SelectRandomNumber(0, numbers.Count);
        Console.WriteLine(numbers[randomIndex]);
        numbers.Remove(numbers[randomIndex]);
    }

}

public int SelectRandomNumber(int fromNumber , int toNumber)
{
    Random r = new();
    return r.Next(fromNumber, toNumber);
}

@TonyTroeff
Copy link
Member Author

This solution is not optimal as the numbers.Remove(..) call has linear complexity - that makes the overall complexity O(n^2).
This is a good starting point: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Also - the RandomizationHelper class should be used for generating the random index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature Something that should be implemented good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants