Random seed not changing with randomize()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Micah Denn
:warning: Old Version Published before Godot 3 was released.

I have two buttons.

The first button generates a matrix with random numbers and sets the tiles on a tilemap.

The second resets the matrix values to 0 and redraws the tilemap.

However every time I press the first button it draws the same tilemap not a map with a new set of random values.

extends Node2D

var matrix = []

var column = 40
var row = 40

onready var button0 = get_node("Button")
onready var button1 = get_node("Button1")

func _ready():
	button0.connect("pressed", self, "draw")
	button1.connect("pressed", self, "clear")
	
func draw():
	generate_matrix()

func clear():
	for x in range(0, column):
		for y in range(0, row):
 			get_node("TileMap").set_cellv(Vector2(x, y), 0)

func generate_matrix():
	for x in range(0, column):
		matrix.append([])
		for y in range (0, row):
			randomize()
			var rand_value = int(rand_range(0, 2))
			matrix[x].append(rand_value)
			var tile_value = matrix[x][y]
			get_node("TileMap").set_cellv(Vector2(x, y), tile_value)

Am I just using “randomize()” incorrectly?

Actually this was happening because I messed up the matrix.

I added matrix = [] to the beginning of the generate_matrix function so it would reset properly.

Micah Denn | 2016-12-27 19:34

:bust_in_silhouette: Reply From: Sprowk

I’m not sure how it works in godot but in python you put randomize() at start of your program.