In my head the below code should pick a random item from the array's twice, instead it picks a random item once and prints it twice. Any ideas where I'm going wrong.

extends Node

var card : = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
var suit : = ["Clubs", "Diamonds", "Spades", "Hearts"]

func _ready():
	randomize()
	
	var random_suit = suit[randi() % suit.size()]
	var random_card = card[randi() % card.size()]
	var loopcount : int = 0
	
	while loopcount < 2 :
		print(random_card, " ", random_suit)
		loopcount = loopcount + 1

So, when you want to post code on the forum, you have to wrap it in tags so it displays properly (I fixed it for you this time). So in the line directly above and directly below the code, type in this (3 tildes without the quotes): "~~~" In terms of your question. When you set the variable like "random_card" that is evaluated once and then the value you set (the random value) is saved in that variable name. When you access the variable again, it is not re-evaluated, it just prints the value you originally saved. If you want the value to be dynamic, and change every time, then you need to make a function. When you call a function, it is evaluated when you call it, so the value will always be new. For example:

func get_random_value(list):
    return list[randi() % list.size()]

Then you can call it like this:

while loopcount < 2 :
    var random_card = get_random_value(card)
    var random_suit = get_random_value(suit)
    print(random_card, " ", random_suit)
    loopcount = loopcount + 1

Also, you can loop easier like this:

for i in range(2):
   # do something twice

Hope that helps.

The function isn't technically necessary, you just need to give the variable it's value inside the loop instead of outside, but it is more generic and reusable that way.

It works! thanks for the help and for the tip about using ~~~ I'll keep it in mind going forward.

a year later