Fast, clean, non-duplicating array randomiser. This rules.
Courtesy: J Stadolnik.Code:shuffle(int cards)
{
int x,tmp;
x=cards;
while(x--) CARDS[x]=x%52;
while(cards)
{
x=random(cards--);
tmp=CARDS[cards];
CARDS[cards]=CARDS[x];
CARDS[x]=tmp;
}
}
Printable View
Fast, clean, non-duplicating array randomiser. This rules.
Courtesy: J Stadolnik.Code:shuffle(int cards)
{
int x,tmp;
x=cards;
while(x--) CARDS[x]=x%52;
while(cards)
{
x=random(cards--);
tmp=CARDS[cards];
CARDS[cards]=CARDS[x];
CARDS[x]=tmp;
}
}
It also ruled when published in Algol 45 years ago. See Knuth's 'The Art of Programming'
Do you have that code in VB by any chance?
I'll try and do a quick translation, I haven't tested it though...
After calling this, the CARDS() array should be re-arranged to a new sequence of numbers.Code:public CARDS(0 to 51) as integer
sub shuffle(byval cards as integer)
dim x as integer,tmp as integer
for x = 0 to cards - 1
CARDS(x) = x mod 52
next x
while cards > -1
'pick random card
x = int(rnd*cards)
cards = cards - 1
'swap cards
tmp = CARDS(cards)
CARDS(cards) = CARDS(x)
CARDS(x) = tmp
loop
End Sub
As i said its not tested so it might be a bit buggy, just compare the code line for line with the C++ version to see the translation.
If you just want to shuffle a 1D string array maybe this will work?
VB Code:
Private Sub Command1_Click() Dim s(9) As String, i&, randomIndex&, tempVal$ For i = 0 To 9 s(i) = "Cogigate " & i & " times" Next For i = 9 To 0 Step -1 Do Randomize randomIndex = Int(10 * Rnd) Loop Until randomIndex <> i 'Now we have the current index and a random index that are different 'Swap the values of the random index and the current index tempVal = s(i) s(i) = s(randomIndex) s(randomIndex) = tempVal Next For i = 0 To 9 Debug.Print s(i) Next End Sub
I did a bunch of work on this type of thing about 20 years ago. I don't have any code, BUT this advice:
If you are really dealing cards (say for a bridge game) generally, the next thing you have to do is to sort them in each hand.
Instead of dealing the cards, deal the SEATS. Then, when you construct the hands the cards are in order.
Seats?
If you start out with 1-52 cards and you have to deal
without replacement you end up with a random placement of
cards in a string/array/whatever. If you have dealt them to 4
players in a card game and you are going to have human
interaction on the screen, you then have to sort them by suit and
denomination. Very time consuming and somewhat tricky.
Assuming that you are dealing to 4 putative players they can be
labelled N,E,W,S (north, east,west,south). In this case, you
randomly determine which of the 4 (or however manyplayers) get
the CARD.
When you end up you have the original deck 1-52 in order, and a
string or array of WHO GETS THE CARD. To build the presentation
hand, you then only have to walk down the list assigning each.
There are also some significant gains in the actual mechanics of
the logic of computer play (ex: second hand low, cover an honor
with an honor, .....)