I need a code to wait between .1 - .3 seconds:
wait(math.random(.1,.3))
So I would assume this would pick a number between .1 and .3, but the thing is going ridiculously fast.
So then I just put into the output:
print(math.random(.1,.3))
Every single time I get 0. I need to know why this is and how I can actually randomly get a decimal value between .1 and .3.
If I recall correctly, math.random only returns whole values.
math.random(10,30)/100)
The new Random
feature supports floating point min/max values:
local random = Random.new() local number = random:NextNumber(0.1, 0.3)
I'm aware that an answer has already been accepted, but I believe this to be the better solution because Random
produces better random numbers than math.random
currently, the syntax of Random.NextNumber
is more intuitive, and the resulting random number will be more granular than dividing a random integer by 100 as in the previously accepted answer.
math.random
has three modes:
math.random(low, high)
produces an integer between (inclusively) integers low
and high
math.random(high)
is the same as math.random(1, high)
math.random()
produces a real number between 0 (inclusively) and 1 (exclusively).You can "shrink" and "shift" the interval [0, 1)
to become [.1, .3)
:
[0, 1) * 0.2
is [0, 0.2)
. [0, 0.2) + 0.1
is [0.1, 0.3)
.
In general, you can use the idiom low + (high - low) * math.random()
to generate real numbers between low
and high
:
for i = 1, 10 do print(0.1 + 0.2 * math.random()) end -- 0.26803754335269 -- 0.17887658532709 -- 0.25661984467879 -- 0.25968800662085 -- 0.28232947150245 -- 0.13951027384028 -- 0.16704455111176 -- 0.25364591889083 -- 0.15555494213477 -- 0.21079399110749