six2six/fixture-factory — pull request #70 by Tavio (2014-12-25 20:24:07+00:00)
Title: Unique random function

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.