in reply to Re: Re: Re: Re: Re: Choose random element of array?
in thread Choose random element of array?


As for feeling bad about splice, you should never feel bad about using tools...

Actually I was joking about the consequences of knowing full well you are doing something inefficiently, but - the splice thing in here:
$randstring .= splice(@testarray, int rand @testarray, 1)
was to ensure always getting a random element from @testarray that also was unique (that is, previously unused), through the loop that constructs the final $randstring..

Will this be the case with the shuffle/Fisher-Yates way?

Replies are listed 'Best First'.
Re^7: Choose random element of array?
by Somni (Friar) on May 29, 2004 at 08:52 UTC

    Remember, I said the loop you had was a shuffle. Both the Fisher-Yates and yours are examples of shuffle algorithms. As such, each element in the original data structure is copied exactly once into the result, in a random position.

    So, yes, that would be the case with the Fisher-Yates algorithm.