-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
public UniqueRandomFunction(int minValue, int maxValue) { | ||
this.dataset = this.initIntegerDataset(minValue, maxValue); | ||
this.shuffleDataset(); | ||
this.nextValueIndex = 0; |
There was a problem hiding this comment.
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?
…erification to the integer range constructor
Looks good. I just believe that we should thrown an exception when there is no more possible values. What do you think? |
Or maybe I could reset the counter and repeat the same values again. What do you think would be best? |
If you repeat the same values again, then it isnt unique. I believe that
|
…generate a value after all possible values have already been generated.
@Tavio as we talked, can you add some docs about 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 |
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.