To shuffle an array a of n elements:
for i from n - 1 downto 1 doUse Random class in java to generate random number.
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
http://techpuzzl.wordpress.com/2010/06/30/card-shuffle/
for i from n - 1 downto 1 doUse Random class in java to generate random number.
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
example for shuffling a pack of cards :
ReplyDeletefor(i=1;i<52;++i)
{
int r=rand()%(i+1);
swap(a[i],a[r]);
}