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

Introduce shuffleList and shuffleListM #140

Merged
merged 2 commits into from
Jan 27, 2024
Merged

Introduce shuffleList and shuffleListM #140

merged 2 commits into from
Jan 27, 2024

Conversation

lehins
Copy link
Contributor

@lehins lehins commented Jul 27, 2023

As it was described in #139 this PR implements the most basic version of random list shuffling, both stateful and pure implementations.

The way the shuffling is achieved is somewhat similar to the implementation in QuickCheck. There are better ways, but the one that I know of requires a mutable array, which would make a dependency footprint or implementation quite a bit more complicated.

We can experiment with performance and quality of alternative implementations for random-1.3, whenever that will come. Unless someone is willing to provide a better implementation (with some becnhmarks) soon enough as a PR into this branch, I think current implementation is sufficiently good enough for the initial version.

Changes in this PR are non-breaking, so I'll happily backport them into a minor random-1.2.2 release.

As a nice extra this PR also contains a pure implementation of uniformList

Copy link
Contributor

@Bodigrim Bodigrim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Shimuuar
Copy link
Contributor

In general +1 but I have few comments. What are stability guarantees for the shuffle? Do we promise to change algorithm output only on major updates?

Bias

Shuffle is very slightly biased. For example let look at shuffle of [a,b]. List of indices (uniformListM n gen) will yield 2^N ties out of 2^2N possible options. Sort is stable so [a,b] will have probability of 1/2 + 2^{-N} and [b,a]1/2 - 2^{-N}.
Bias is negligible ~5e-20 in 64-bit case and small ~2e-10 in 32-bit case.

Analysis for n-element list is more complicated. Probability of having at least one collision in indices is subject to birthday paradox. So maybe it's possible to detect bias with 32-bit Ints. And yes Fisher-Yates shuffle is unbiased, has O(n) complexity but requires in-place mutation. It's awfully inconvenient to not having access to proper arrays

P.S. I'm not sure that uniformList warrants inclusion since we have replicateM but I'm not against it

@lehins
Copy link
Contributor Author

lehins commented Jul 31, 2023

Shuffle is very slightly biased. ..... It's awfully inconvenient to not having access to proper arrays

That's precisely why I didn't bother writing it using mutable arrays. I don't think this issue alone warrants a dependency on primitive.

What are stability guarantees for the shuffle? Do we promise to change algorithm output only on major updates?

Yes. That would be the case. I would rather not change the underlying semantics on non-breaking version updates.

@Shimuuar
Copy link
Contributor

I'm on a fence about this. Arrays are after all very basic functionality. It's first time we run into algorithm that requires arrays. Will it be another one? Something that requires lookup tables, such as mwc-random's normal.

@Bodigrim
Copy link
Contributor

Bodigrim commented Jul 31, 2023

If you like pain, you can use naked Data.Array.Byte, it should be enough to shuffle a list of indices. Otherwise depending on array is less of a burden than primitive, because array is preinstalled as a boot package anyways.

@lehins
Copy link
Contributor Author

lehins commented Jul 31, 2023

Otherwise depending on array is less of a burden

I'd rather use my own redefinition of data Array a = Array (Array# a) then suffer through using array package 😄

@lehins lehins force-pushed the lehins/shuffleList branch from 33e4226 to 4d212cc Compare January 27, 2024 09:47
@lehins lehins changed the title Introduce uniformList, shuffleList and shuffleListM Introduce shuffleList and shuffleListM Jan 27, 2024
@lehins lehins merged commit 74146bd into master Jan 27, 2024
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants