0 votes

So I know how to get random numbers, but I am having a problem, I am trying to get random numbers out of a dictionary. Like this:

Var randDictionary = {1:rand_range(0,2)}

func getRand():
     Randomize()
     var Num = randDictionary[1]

how ever it returns the same number every time. Is there a better way to do this? If not I was thinking of having a array inside that dictionary that holds the random ranges and the function calls that, would that be the best way to do it?

in Engine by (61 points)

It depends, are you trying to get a random number or a random number from a predefined set(like from a predefined set of numbers {0,5,8,13,15,16} )?

1 Answer

+4 votes

It's returning the same number every time because you are randomizing after the dictionary value is set.

var randDictionary 

func _ready():
    randomize()
    randDictionary = {1:rand_range(0,2)}
    getRand()

func getRand():
     var num = randDictionary[1]
     print(num)
by (1,921 points)

thank you it worked!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.