The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+54 votes

I dont remember what language it was but you could do random(1,10) or something like that and it would pick a number between 1 to 10, is there a way to do something like this in gdscript?

in Engine by (483 points)

I think that language was Python.

13 Answers

+27 votes

yes. rand_range(float from, float to)

use randomize() before for "true" random numbers

by (429 points)

Okay thanks for the help ^^

I like this answer better because it mentions the randomize function, which without it, chooses the same "random" numbers every time.

Thanks for mentioning randomize. Is there an approach that permits to specify a seed number?

RandomNumberGenerator in Godot 3.1 allows to set any seed for each instance, so you could have independent "deterministic" generators in GDScript for different purposes within a game (randomizing different set of independent features).

+67 votes

randi()%10+1 returns an int from 1 to 10

randf()*10.0+1.0 returns a float from 1.0 to 10.999999~

rand_range(1,11) returns a float from 1.0 to 10.999999~

range(1,11)[randi()%range(1,11).size()] is a little ugly and less efficient but returns an int from 1 to 10 without you having to do the math yourself (aka the 11+1 part) because all you need to do is set the range()

by (855 points)
reshown by

Answer changed due to more info

Yay I won! Take that tiernich!

lol - defeat accepted :D

To get a random integer between 1 and 10, I find it more intuitive to floor the random float in range. More specifically:
floor(rand_range(1, 11))

randi()%11+1 is probably biased towards lowest values of 1..10

I'm trying to generate a random between 10-30 (randi()%30+10) but it's giving me a 35. Probably doing it wrong, but can't figure out how to make it right.

try (randi()%30)+10

Don't forget to call randomize

This answer has an important error. If you want to produce a real number in the interval [x,y[, in general you use:

(randf() * (y-x)) + x.

From 1 to 10, you have randf() * 9 + 1. This is because randf() can be 1 as maximum. In this case, you would obtain 9 + 1 = 10. In the case proposed by this answer, randf() * 11 + 1, if by chance randf() turns out to be greater than 0.8 you get something greater than 10. So, there is a 20% chance of having a wrong answer.

randi()%11+1 returns an int from 1 to 11

If you someone needs a random int between 1 and 10, use:

Please note that randi()%11+1 return a number between 1 and 11 not 10
because randi()%11 can be 10 and adding 1 to it will make it 11. the same thing for randf()

yes indeed, obviously and i don't know how we could go so wrong

+7 votes

As of Godot 3:

randi() % 20 returns an int from 0 to 19

randi() % 20 + 1 returns an int from 1 to 20

randf() returns a float from 0 to 1

Unfortunately, the older style no longer seems to work.

by (56 points)
edited by

Just a correction on the third line.

randi() % 20 + 1 won't return an int from 0 to 20.

It'll be an int from 1 to 20. Still very helpful, thanks.

+7 votes

The following code returns a random number, but it is the same number each time the script is executed:

var random_value = rand_range(1,10)

The following code gives you different random numbers each time the script is executed:

var random_generator =
var random_value = random_generator.randf_range(1,10)

or simply:

var random_value = rand_range(1,10)
by (332 points)
edited by

You don't need to call randomize() every time you pick a random number. Just call it once after starting the project.

Oh. You are right. We don't need to call randomize() in every script.

+1 vote

I needed a rand_range(a, b) but with integers, also to work with negative numbers. I came up with this function.

func randi_range(from, to):
    if from > to:
        var old_from = from
        from = to
        to = old_from
    return int(floor(rand_range(from , to + 1)))
by (16 points)
0 votes

You can use key words like randomize for randomizing numbers from 0 to 2^32, randrange for randomizing an specific range of numbers, for example randrange(2, 20) will return 2,3,4,5,6...19 or 20. There also at options like randi () or randf but I think those are the more important. I’ll link rand_range since is the specific one in your case:

by (14 points)
+1 vote

var name = 0

func ready():
name = rng.randi
range(1, 10)

The random number will be between 1 and 10.

by (254 points)
+2 votes

Yea, the best way i found out how is this way:

    var RNG =

func _ready():
    var rannum = RNG.randf_range([number], [number])

So that's the code. hopefully it helps!

by (27 points)
+1 vote

rand_range(float from, float to)

use randomize() before for "true" random numbers

by (16 points)
reshown by
0 votes

use randomize();

by (14 points)
0 votes

you could use the
if you want a float

or if you want and int

(I think I wrote that right)

I dont think you need to use the randomise() function, please correct me if I am wrong

by (16 points)

randf_range() , randi_range() - only works in Godot 4

+1 vote

Landed here wanting to find a basic Rand() function that can return a number between low and high. Supports OP original ask in addition to anyone looking for a custom low besides 1. This implementation supports negatives. It hits the low and high without going above or below.

var randNum = Rand(-10, 20)

func Rand(low, high):   
    return floor(rand_range(low, high + 1))
by (44 points)
0 votes

In Godot 4, use randf_range(from, to) and randi_range(from, to). They each take a minimum and maximum value, but one is for floats and the other for integers.

by (154 points)
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 Frequently asked questions and 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 [email protected] with your username.