## Recommended Posts

Hi everyone im making a card game which needs a random generator to select 21 cards out of 52

Ive tried playing around with auto-it3s samples, but I cant get them to work for what I want to do

What I need is to randomly generate 21 numbers, with out the generator generating the same number

a few times out of the 21.

If anyone can post a script that does that, I'll really appreciate it.

##### Share on other sites

```Opt("MustDeclareVars", 1)

_Main()

Func _Main()
Local \$deck = _InitDeck()
Local \$a_cards = _Deal(21, 52)
Local \$s_text = "", \$i
For \$i = 1 To \$a_cards[0]
\$s_text = \$s_text & "card #" & \$i & ": " & \$deck[\$a_cards[\$i]] & @LF
Next
MsgBox(0, "Dealt", \$s_text)
EndFunc   ;==>_Main

Func _Deal(\$i_howmany = 1, \$i_cards = 52)
Local \$i, \$x, \$used, \$card
Local \$a_cards[\$i_howmany + 2], \$i
\$a_cards[0] = \$i_howmany
For \$i = 1 To \$a_cards[0]
\$card = Random(1, \$i_cards, 1)
\$used = 0
For \$x = 1 To \$a_cards[0]
If \$a_cards[\$x] = \$card Then
\$used = 1
ExitLoop
EndIf
Next
If Not \$used Then
\$a_cards[\$i] = \$card
Else
\$i -= 1
EndIf
Next
Return \$a_cards
EndFunc   ;==>_Deal

Func _InitDeck()
"Ace Hearts,2 Hearts,3 Hearts,4 Hearts,5 Hearts,6 Hearts,7 Hearts,8 Hearts," & _
"9 Hearts,10 Hearts,Jack Hearts,Queen Hearts,King Hearts," & _
"Ace Clubs,2 Clubs,3 Clubs,4 Clubs,5 Clubs,6 Clubs,7 Clubs,8 Clubs," & _
"9 Clubs,10 Clubs,Jack Clubs,Queen Clubs,King Clubs," & _
"Ace Diamonds,2 Diamonds,3 Diamonds,4 Diamonds,5 Diamonds,6 Diamonds,7 Diamonds,8 Diamonds," & _
"9 Diamonds,10 Diamonds,Jack Diamonds,Queen Diamonds,King Diamonds", ",")
EndFunc   ;==>_InitDeck```

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

Hi everyone im making a card game which needs a random generator to select 21 cards out of 52

Ive tried playing around with auto-it3s samples, but I cant get them to work for what I want to do

What I need is to randomly generate 21 numbers, with out the generator generating the same number

a few times out of the 21.

If anyone can post a script that does that, I'll really appreciate it.

Here is one way

```#include <Array.au3>

\$numCardsToGet = 21

\$array = GetRandomCards(\$numCardsToGet)

_ArrayDisplay(\$array, "Random Cards")

Func GetRandomCards(\$numCardsToGet)
Const \$NUM_CARDS_IN_DECK = 52
Const \$NUM_TIMES_TO_SWAP = 1000

Local \$deck[\$NUM_CARDS_IN_DECK + 1]

For \$i = 1 To \$NUM_CARDS_IN_DECK Step 1
\$deck[\$i] = \$i
Next

For \$i = 1 To \$NUM_TIMES_TO_SWAP Step 1
_ArraySwap(\$deck[Random(1, \$NUM_CARDS_IN_DECK, 1)], \$deck[Random(1, \$NUM_CARDS_IN_DECK, 1)])
Next

ReDim \$deck[\$numCardsToGet + 1]
\$deck[0] = UBound(\$deck) - 1

Return \$deck
EndFunc```

##### Share on other sites

Thank heaps GAFROST . now I can proceed with my script.

##### Share on other sites

Thanks heaps CREEPING, yours and GAFROSTs post are AWSUM

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...