I have a fully populated array of values, and I would like to arbitrarily remove elements from this array with more removed at the far end.
For example, a given input (where a. Denotes a populated index)
............................................
I would like something like
....... . ... .. . . .. . .
My first thought was to count the elements, and then iterate over the array, creating a random number somewhere between the current index and the total size of the array, for example:
if ( mt_rand( 0, $total ) > $total - $current_index )
however, since this entails creating a random number every time the loop goes around it, it becomes very difficult.
Is there a better way to do this?
source share