Card Shuffling Function

Can I see some events?

To generate a random non repeated number, you can use:

  • A structure with (initially) 52 childrens, that let you know wich values you have already used. For example, the first dealt card is a random number between 1 and 52, let’s say it was 10 (10 of Diamonds): Create the dealt object with the animation 10 (the value of the children), “move” the last children (52 = K of Hearts) to the position of 10 (set structure[“10”] = 52), and finally delete the children 52. Now you have a structure with 51 childrens, without the 10 of Diamonds, because the children 10 has the value of 52. The next card to deal has to be a random number between 1 and 51, without the risk to generate a repeated number:

Deck: Deck: Deck: 1 1 1 1 1 1 2 2 2 2 2 2 ... ... ... ... ... ... 10 10 <-- you take this 10 52 <-- move last child here 10 52 ... ... ... ... ... ... 52 52 52 52 51 51 <-- delete last child
You’ll need an extra variable “size” so keep track the structure length, if the new expressions to get the structure size don’t exist in your GD version :wink:

  • A while loop (brute-force-like), you generate a random number, if the generated number is already a dealt card, generate a new one, check, and so on:

Conditions: Conditions to deal a card Actions: Do = 1 to variable already_dealt // just to start the while loop //Sub-event While: Variable already_dealt is = 1 Conditions: No conditions Actions: Do = Random(52) to variable random_card // generate a new random number Do = 0 to variable already_dealt // non repeated by default //Sub-event For each DealtObject, repeat: // now let's check if really is not repeated, against every dealt object Conditions: Animation of DealtObject is = Variable(random_card) //oops, it is repeated Actions: Do = 1 to variable already_dealt // set as repeated, so the while loop generates a new number
Note that the While loop will need more loops if there are a lot of dealt cards, for example if you have only one card in the deck (51 cards dealt), in average the while loop will need 52 loops to get that last card number.

I can help more if you show me some events, so I can tell you where to place the events, and use some already created variables :slight_smile: