RNG pattern problem

Godot Version

4.4

Question

Im trying to do a random card generator for my game, but the rng doesnts seem to work properly, i see a pattern where some cards apear almost allways, some almost never, its really weird couse i dont know if its just me or this actually has a pattern even using randomize() in every call to the function

here is the code: (for context, the while exists loop is to keep generating cards until it founds one that isnt already en the 6 slots i have)

func rand_joker():
	randomize()
	var exists = true
	var legendary = [
		"res://sprites/tarot/the_world.png"
	]
	
	var epic = [
		"res://sprites/tarot/the_star.png",
		"res://sprites/tarot/the_chariot.png",
		"res://sprites/tarot/the_magician.png",
		"res://sprites/tarot/the_hermit.png",
		"res://sprites/tarot/the_hierophant.png",
		"res://sprites/tarot/temperance.png",
		"res://sprites/tarot/justice.png"
	]
	
	var rare = [
		"res://sprites/tarot/death_13.png",
		"res://sprites/tarot/the_hanged_man.png",
		"res://sprites/tarot/the_tower.png"
	]
	
	var common = [
		"res://sprites/tarot/the_fool.png",
		"res://sprites/tarot/the_devil.png",
		"res://sprites/tarot/the_moon.png",
		"res://sprites/tarot/the_high_priestess.png",
		"res://sprites/tarot/the_sun.png"
	]
	
	var card = ""
	while exists:
		var rng = RandomNumberGenerator.new()
		var rand = rng.randi_range(1, 100)
		if rand <= 3:
			randomize()
			card = legendary[rng.randi_range(0, legendary.size() - 1)]
		elif rand <= 15:
			randomize()
			card = epic[rng.randi_range(0, epic.size() - 1)]
		elif rand <= 45:
			randomize()
			card = rare[rng.randi_range(0, rare.size() - 1)]
		else:
			randomize()
			card = common[randi_range(0, common.size() - 1)]
		if load(card) == $tarot1.texture or load(card) == $tarot2.texture or load(card) == $tarot3.texture or load(card) == $tarot4.texture or load(card) == $tarot5.texture or load(card) == $stash.texture:
			exists = true
		else:
			exists = false
	return card

You are creating a new random number generator each loop, using randomize() on the global RNG, these two things are strange but shouldn’t affect the odds. It may be better to create only one RandomNumberGenerator or use the global randi_range, without calling randomize() so often.

Pure random is exactly that, in your small sample size you may never see certain cards, in fact it’s quite likely; you are just lucky!

1 Like

i tested generating 100 in a row and rng seems to work fine now, ig it was just me seeing patterns when they arent any… thx for the help anyway!