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

Unique random function #70

Merged
merged 7 commits into from
Jan 5, 2015
Merged

Unique random function #70

merged 7 commits into from
Jan 5, 2015

Conversation

Tavio
Copy link

@Tavio Tavio commented Dec 25, 2014

When using Fixture Factory I felt the need for a function capable of generating unique random values (that is, a function that generates in each call a value different from those it has already generated in previous calls).

I created then a function which can generate unique random values from a fixed array of values, from a range of integers, or from the values of an Enum.

The method used to guarantee that each call to generateValue() produces an unique value is the Fisher-Yates shuffle (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm). When the unique random function is initialized, it generates an array composed of all the possible values that can be generated and then shuffles the array using the FIsher-Yates method. The function then returns those values in order, as they are requested.

The drawback of this is method is that it may consume too much memory if the user specifies a large range of values as the dataset. However, I have chosen not to impose a fixed limit to this range, for two reasons: firstly, I wouldn't know what arbitrary number to choose for the limit; secondly, I believe that whoever wants an unique sequence of values probably doesn't want too many values to be generated (the use case I have in mind is something like choosing from a list of countries, for instance).

I also chose not to extend the existing RandomFunction because I felt that there is nothing in RandomFunction that my new function could reuse.

public UniqueRandomFunction(int minValue, int maxValue) {
this.dataset = this.initIntegerDataset(minValue, maxValue);
this.shuffleDataset();
this.nextValueIndex = 0;
Copy link
Member

Choose a reason for hiding this comment

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

I believe we could set initial nextValueIndex right on attribute declaration, to avoid repeating it in all constructors. What do you think?

@nykolaslima
Copy link
Member

Looks good. I just believe that we should thrown an exception when there is no more possible values. What do you think?

@Tavio
Copy link
Author

Tavio commented Dec 27, 2014

Or maybe I could reset the counter and repeat the same values again. What do you think would be best?

@nykolaslima
Copy link
Member

If you repeat the same values again, then it isnt unique. I believe that
users will use this unique in very specific situations and they know that
values cannot repeat. I think that throwing exception is the best solution.
On sex, 26 de dez de 2014 at 23:49 Tavio notifications@github.com wrote:

Or maybe I could reset the counter and repeat the same values again. What
do you think would be best?


Reply to this email directly or view it on GitHub
#70 (comment)
.

…generate a value after all possible values have already been generated.
@nykolaslima
Copy link
Member

@Tavio as we talked, can you add some docs about uniqueRandom in README?

If there is an error, like no more possible values, it will be hard to user to find out what field has the problem. But I think that's a problem with our structure. Maybe we should have something like FunctionException that need to be caught outside function scope to be logged. But we can do this in other pull request.

@nykolaslima
Copy link
Member

As discussed with @Tavio and @ahirata we will merge it. This will cycle the values if all possible values was already generated.

Let's see how people use it.

nykolaslima added a commit that referenced this pull request Jan 5, 2015
Unique random function
@nykolaslima nykolaslima merged commit 53d2a23 into six2six:master Jan 5, 2015
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