Matu Posted September 30, 2006 Posted September 30, 2006 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.
GaryFrost Posted September 30, 2006 Posted September 30, 2006 expandcollapse popupOpt("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() Return StringSplit("Ace Spades,2 Spades,3 Spades,4 Spades,5 Spades,6 Spades,7 Spades,8 Spades," & _ "9 Spades,10 Spades,Jack Spades,Queen Spades,King Spades," & _ "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 SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
creeping Posted September 30, 2006 Posted September 30, 2006 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
Matu Posted October 1, 2006 Author Posted October 1, 2006 Thank heaps GAFROST . now I can proceed with my script.
Matu Posted October 1, 2006 Author Posted October 1, 2006 Thanks heaps CREEPING, yours and GAFROSTs post are AWSUM
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now