Solution :
For every i, card at a random location is placed at i
and card that was at i is placed at j.
If we want every card to change its previous location -
For every i,
Generate a random location >i and which is not same as i
public void shuffle()
{
int[] cards =new int[52];
Random random =new Random();
random.setSeed(new Date().getTime());
for(int i=0,j=0;i<52;i++,j++)
{
cards[i]=j;
System.out.println(j);
}
for(int i=0,j=0,t=0,k=0;i<52;i++)
{
//For every i, card at a random location is placed at i
//and card that was at i is placed at j
j=random.nextInt(52);
t=cards[j];
cards[j]=cards[i];
cards[i]=t;
}
for(int i=0;i<52;i++)
{
System.out.println(cards[i] + ", "+i);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment