2 condition IF Statement/randomising numbers

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

so I’m creating a game that basically displays a customer, and the texture of the customer randomises every time the ‘next costumer’ button is pressed, i want to make it so that every time the ‘next customer’ button is pressed it randomises an integer between 1 and 10 and if the number is more than 1 then the photo of the costumer’s ID will match the texture of customer and if the number is equal to 1 then it will display a different photo

my issue is every time i press the ‘next customer’ button no matter how many times it always seems to be the same photo as the customer’s texture and never seems to display the incorrect one

my code so far:

var num = null

func _on_NXTButton_pressed():
	dd = ''
	mm = ''
	yy = ''
	_ready()
	variate_texture()
	num = randi() % (1-10)
	#$NXTButton.visible = false
	$Booth2D/PassSprite.visible = true
	$Person1.visible = true


func variate_texture():
	if text_array.size()>1:
		var texture_id: int = randi() % text_array.size()
		var chosen_texture: Texture = text_array [texture_id]
		new_text = chosen_texture
		$Person1.texture = new_text
		if new_text == Elisa and num <= 1:
			$Booth2D/PassSprite/PassPhotoSpr.texture = ElisaP
		elif new_text == Elisa and num > 1:
			$Booth2D/PassSprite/PassPhotoSpr.texture = EricP

could it be to do with the way im randomising the numbers ?

Edited to fix code formatting…

jgodfrey | 2023-01-12 16:39

:bust_in_silhouette: Reply From: dap404

make sure at the top to add(this resets the randomness seed)

randomize()

for example to get a random number you would do something like this

func get_random_number():
    randomize()
    return randi()%11

Though, note, you really only need to call randomize() once per app instance (so, for example, in the _ready() function of the main scene). It certainly doesn’t hurt to call it multiple times, but it’s really not necessary.

jgodfrey | 2023-01-12 19:56

perfectly stated jgodfrey ^^^
ziggy please mark correct if it worked out

dap404 | 2023-01-18 20:45