I have an array in my PHP application, and I need to develop a function that, given the array and an arbitrary string password, will output a shuffled subset of the large array. The subset and order of the array outputted should be the same every time the same password is used to call the function.
I can generate a pseudo-random number based on the password by taking the integer value of a hash like MD5, but I'm not sure how I can use this to then:
1) Select X (either a fixed number or, preferably, a number chosen by the hash within a given range) random items from the array based on the hash
2) Shuffle/rearrange these items into a subset based on the hash
For my implementation collisions are acceptable, as the array is of a relatively small size. This is a pretty interesting problem - anyone have any idea how to tackle it?
Don't know much about the PHP implementation, but I think the general algorithm would involve seeding a pseudo-random number generator using your hash.
Seeding a pseudo-random number generator ensures that the pseudo-random numbers you get are in the same order every time. Shuffle the array using the first pseudo-random number from the seeded generator. Then, use the next value of the pseudo-random number generator to pick a number between 1 and the size of your array; slice off these first n
elements from the array.