How would i go about picking a random number?

Attention Topic was automatically imported from the old Question2Answer platform.
Old Version Published before Godot 3 was released.

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?

I think that language was Python.

toivocat | 2021-09-08 20:54

yes. `rand_range(float from, float to)`

use `randomize()` before for “true” random numbers

Okay thanks for the help ^^

The_Duskitty | 2016-04-02 18:55

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

Oranjoose | 2016-04-16 20:28

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

hansolo | 2017-09-07 10:52

`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).

Xrayez | 2019-06-06 22:52

`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()

The_Duskitty | 2016-04-02 23:44

Yay I won! Take that tiernich!

batmanasb | 2016-04-03 01:23

lol - defeat accepted

tiernich | 2016-04-03 03:13

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))

Oranjoose | 2016-04-16 20:32

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

Omicron | 2017-03-08 09:35

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.

Rodrigo Maller Marti | 2017-05-24 00:11

try (randi()%30)+10

Omicron | 2017-05-24 05:48

Don’t forget to call randomize

jarlowrey | 2017-06-01 15:39

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.

VictorSeven | 2017-09-05 18:00

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

If you someone needs a random int between 1 and 10, use:
randi()%10+1

leo_frozen | 2018-11-09 02:20

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()

kamel | 2020-04-30 15:45

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

Omicron | 2020-05-03 09:07

Corrected the inaccuracies. The random floats won’t return a 1.0, but at most .999999~.

The last method using arrays I left, but would recommend `int(rand_range(1,11))` as an alternative.

avencherus | 2020-05-05 13:47

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.

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.

idkaryan | 2019-09-11 10:26

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 = RandomNumberGenerator.new()
random_generator.randomize()
var random_value = random_generator.randf_range(1,10)
``````

or simply:

``````randomize()
var random_value = rand_range(1,10)
``````

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

Calinou | 2020-05-23 13:27

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

luislodosm | 2020-05-23 13:45

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)))
``````

You can use key words like randomize for randomizing numbers from 0 to 2^32, rand_range for randomizing an specific range of numbers, for example rand_range(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: https://docs.godotengine.org/es/stable/getting_started/scripting/c_sharp/c_sharp_differences.html?highlight=Rand_range#random-functions

var name = 0

rng.randomize()
name = rng.randi_range(1, 10)

The random number will be between 1 and 10.

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

``````    var RNG = RandomNumberGenerator.new()

RNG.randomize
var rannum = RNG.randf_range([number], [number])
``````

So that’s the code. hopefully it helps!

rand_range(float from, float to)

use randomize() before for “true” random numbers

use randomize();

you could use the
`randf_range(0,10) `
if you want a float

`randi_range`
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

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

rustam2704 | 2023-02-12 02:18

``````var randNum = Rand(-10, 20)