What am I misunderstanding about randomize() or randi()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By monny

I’ve tried to work it out myself but I’m just stuck on what I’m misunderstood.

The code:

func _ready():	
randomize()
for i in range(3):
	Agent["Name"] = Names[randi() % Names.size()]
	Agent["Persuade"] = randi() % 10 + 1
	Agent["Combat"] = randi() % 10 + 1
	Agent["Stealth"] = randi() % 10 + 1
	Agent["Hacking"] = randi() % 10 + 1
	AgentList.append(Agent)
	i += 1

The result:

[{Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}, {Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}, {Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}]

Each time I run the program it will generate different results, but within the actual generation the numbers will always be the same. I want them to be different for each agent.

:bust_in_silhouette: Reply From: BraindeadBZH

Your issue has nothing to do with the random functions of GDScript.

You are appending three time the same “Agent” object. The list is not creating a copy, it only store a reference to the object. So in reality in each pass of the loop you are modifying the same object. You have to instantiate a new object at the beginning of the for.