← Pixel Dreamer Studio

A fair shuffle you can replay

Swap each card only with the cards not placed yet, from your own seeded RandomNumberGenerator.

A hand-written shuffle that swaps every card with any position in the deck is not fair: some orders come up more often than others. In our Godot 4.7 test, 270,000 shuffles of a 3-card deck dealt ACB, BAC and BCA 18.4% of the time and ABC and CAB 14.9%, when each should be 16.7%. The built-in Array.shuffle() was fair, 16.6 to 16.7%. The script, a Fisher-Yates shuffle with its own RandomNumberGenerator, was fair too, 16.6 to 16.8%, and the same seed dealt the same 52-card deck again.

The code

If you only need a fair random order, deck.shuffle() already is one. Use the script when you also want to replay a deal: save it as fair_shuffle.gd and call preload("res://fair_shuffle.gd").shuffle(deck, seed_value) with the same seed for a daily challenge, a replay or a bug report, or without a seed for a new order each time. Array.shuffle() uses the global random generator, so anything else that calls randi() or randf() changes which order you get.

extends RefCounted
## Shuffles a deck so every order is equally likely, with its own seed,
## so a daily run or a replay deals the same cards again.
##   var deck := ["A", "K", "Q", "J"]
##   FairShuffle.shuffle(deck)              # random every time
##   FairShuffle.shuffle(deck, 20261128)    # same seed, same order


static func shuffle(deck: Array, seed_value: int = -1) -> Array:
	var rng := RandomNumberGenerator.new()
	if seed_value >= 0:
		rng.seed = seed_value
	else:
		rng.randomize()
	for i in range(deck.size() - 1, 0, -1):
		var j := rng.randi_range(0, i)  # only from the cards not placed yet
		var tmp = deck[i]
		deck[i] = deck[j]
		deck[j] = tmp
	return deck

What we measured

A 3-card deck, A B C, shuffled 270,000 times with each method, counting how often each of the 6 orders came out. Two runs gave the same numbers.

Why it happens

Swapping each of 3 cards with any of 3 positions makes 3 × 3 × 3 = 27 equally likely paths, but there are only 6 orders, and 27 does not divide by 6, so some orders get 5 paths and others 4: 18.5% and 14.8%. Fisher-Yates swaps each card only with itself or the cards before it that are not placed yet: 3 × 2 × 1 = 6 paths, one per order. Bigger decks have the same problem, because n × n × ... × n paths do not divide evenly by the number of orders.

What to change

What the cards look like when they are played: Card Game VFX & Shaders for Godot 4 has 16 2D particle effects for card games, including Hit Flash, plus shaders for Sprite2D such as Glow Outline and Burn Dissolve.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Card Game VFX & Shaders for Godot 4 has 16 2D particle effects for card games, including Hit Flash, plus shaders for Sprite2D such as Glow Outline and Burn Dissolve.

More how-tos for Card Game VFX & Shaders for Godot 4: Hit flash shader for 2D sprites · 2D outline shader that is not cut off

More how-tos: Game systems