random is generating the same thing every time !!!

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

so i have a series of boxes inside a box container
i want to give each of them one of six colors when scene loads up
here is my code

extends Node2D


var color_one = Color(255, 0, 0, 1) 
var color_two = Color(0, 255, 0, 1) 
var color_three = Color(0, 0, 255, 1) 
var color_four = Color(0, 0, 0, 1) 
var color_five = Color(255, 0, 255, 1)
var color_six = Color(255, 255, 0, 1) 

var colors_array = [color_one, color_two, color_three, color_four, color_five, color_six]

func update_colors():
	var nodes = get_node("box").get_children()
	for box in nodes:
		var current_color = colors_array[randi() % colors_array.size()]
		box.modulate = Color(current_color)
func _ready():
	update_colors()

but strangely its generating the same colors every time!!!

:bust_in_silhouette: Reply From: kidscancode

You need to call randomize() to seed the rng, or you will get the same “random” sequence every time you run.

You can add it to _ready().

1 Like