[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Random array (fwd)




> And what's so bad about doing this:
> 
> for (i=0; i < n-1; i++) {
>   swap_positions(array, i, i+rand(n-i));
> }
>
> which populates the array by randomly choosing the next element?

That one's fine, and is equivalent to Knuth's Algorithm P, except
that it does two more additions per iteration.

-- 
	Jim Gillogly
	Trewesday, 9 Blotmath S.R. 1998, 19:16
	12.19.5.11.12, 10 Eb 5 Zac, Seventh Lord of Night