-
Notifications
You must be signed in to change notification settings - Fork 404
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
Pick does not choose randomly when choosing all elements #355
Comments
It isn't released, yet, but it was fixed in #333. |
I built this locally with fix #333 and it doesn't seem to have solved the issue. The problem is in the
|
Sorry, I didn't read your first post closely. You're right. It doesn't shuffle when https://en.wikipedia.org/wiki/Reservoir_sampling I suppose it would be ideal to have it shuffle when |
As noted: Gen.pick(4, 1 to 4) is constant: (1, 2, 3, 4). Surprising. Unfortunately
This code demonstrates
with the following output
Not very random.
However the ones it omits are distinctly non-random. It omits exactly every pair of the form (All above based on version 1.14.0.) |
True, the lack of shuffling is generally a problem, regardless of length. This function is called "pick", it isn't called "permute". Since modifying For now, maybe it should be documented that |
Better docs of Fully understand the risks associated with "fixing" existing behavior that is arguably more "surprising" than "wrong". But Aside: careful software engineers shouldn't infer semantics from a name alone: "pick" means whatever the implementor intended (documented or not) and what the implementation actually does (of course, we hope they are nearly the same). Good names help us remember the semantics, but precise documentation is always needed As someone just digging into Scalacheck (I love it!) and trying to get a good understanding of generators, and trying to write some new ones (including a permuter and a partitioner) I've been suffering a bit. I've had to write more experimental tests than I would like in order to understand the real semantics of some of the built-in generators, such as our friend The following would be useful information in the docs for each generator:
As currently implemented The current doc is pretty light:
That leaves open questions. Does it pick with or without replacement? What exactly does randomly mean? Does it preserve input order or not?
I could well imagine a different permutation generator with this spec.
Or a picker that preserves order:
|
OK. I went back and read source code a little more carefully. Now I understand why
It then continues to iterate over the remaining So if any of the first This can be captured as a property
The semantics are a little bit complicated, but reasonably concise once the smoke has been blown away. |
I experimented with variant
As noted in the source comments, the algorithmic change from standard Here is an outline of performance comparison tests:
With these results
Does the concept of preserving input order have any merit? Either as
|
The documentation improvements to explain the order issues with Closing this issue, but we can be reopen if it's deemed necessary. |
Pick is defined as:
I use
pick
to randomly choose 3 values from a range of 1 to 5 with the following:We see that the 3 values are chosen randomly.
If I ask for all 5 values to be chosen randomly:
I keep getting the same values.
Is this the expected behaviour? It seems like unless the
n
supplied to pick is less than the size of the iterablei
, it does not return random values.The text was updated successfully, but these errors were encountered: