Try this logic
1. Generate a random number between (say) 50 and 100. (r1)
2. for count = 1 to r1
generate 2 more random numbers between 1 and 52 to be used
as index (i1 and i2)
replace element of i1 element of i2 and vice versa
i.e. swap arrDeck[i1] and arrDeck[i2]
This will guarantee that the values in the array are not touched. And you
are swapping numbers r1 times.
This will always give a nicely shuffled array. Try It.
I would have to think about the implementation but the logic would certainly
work.
-----Original Message-----
From: O'Hara, Elliott M [mailto:EMOHARA@k...]
Sent: Friday, February 22, 2002 2:44 PM
To: javascript
Subject: [javascript] End of the week
So its the end of the week, and I'm not starting anything new...
so I'm playing..
I'm trying to shuffle a deck of cards
The deck (arrDeck[52]) works fine,
but hmm...
I want to simulate a shuffle, in other words, I want 1 - 52 randomly
selected...
but I want them all, and they can't repeat, and I can't beat up the client
cpu...
This is simple and its friday right???
how in the world??? Ideas?
"The information in this email, and in any attachments,
may contain confidential information and is intended
solely for the attention and use of the named addressee(s).
It must not be disclosed to any person without authorization.
If you are not the intended recipient, or a person responsible for
delivering it to the intended recipient, you are not authorized
to, and must not, disclose, copy, distribute, or retain this
message or any part of it."