lundi 23 mai 2016

Assign random but even amount from an array to a list array in PHP?

I have to arrays. One array $People currently creates number of 44 individuals. Lets just assume currently its $People = array('1','2',...,'44');. I have another array $Test = array('A','B',...'O');, this would be length of 15. Now i want to be able to assign the test randomly to each individual. I know how to do this using random function in php.

Where it has got tricky for me and what i need help with is how can i even out the test array. What i mean by this is since there are currently 44 individuals (this array will grow in future), what I want is 14 test versions to have 3 individuals and 1 version would have 2. So i want the test array to even out. I also want it to handle as growth of $People array.

Ex: Test Version D will have individual '4', '25'. Every other version has three random individuals.

Few ideas i came up with are things like running random integer on $Test array and which ever gets highest/lowest gets 2 individuals and rest three. This would give a problem when i increase the size of $People array; to deal with that i though about using modulus to figure out what will be even number of $Test beforehand.

I can do this and other ideas but am pretty sure there has to be a better way to do this.




Aucun commentaire:

Enregistrer un commentaire