print(math.random(2,1))
The above code either prints a 1, or a 2 How may I make it to print a number between x or y? Thank you!
print(math.random(2,1))
The above code either prints a 1, or a 2 How may I make it to print a number between x or y? Thank you!
Use math!
So in order to get a decimal between 1-2 you use math.random(), obviously. Depending on how long of a decimal you want, you'll want to change the number values by a factor of x10. So,
Step one,
Multiply the numbers you want to make into a decimal by 10, 100, 1000, ect.
100,200
Step two,
Use math.random the get a number between these values.
math.random(100,200)
Step three,
Divide by how much you multiplied, in my case 100.
math.random(100,200)/100
And to print this use print(), as you know.
print(math.random(100,200)/100)---prints a decimal in the hundredths place
Some possible outputs could be the following,
0.47
0.83
0.12
Hope that helped!
Good Luck!
While the answer above me will work just as well as the code i am showing you, this code works too.
local x = 1
local y = 2
print(tostring(math.random(x,y,0.01)))
--[[
math.random(x,y,0.01) --the 0.01 tells math.random that it can pick any number to 2 decimal places
tostring(value) --This is good practice and useful if you needed to combine this random value with a string
--]]
--NilLogic
Are you sure? I just checked on a sketch Lua app and it failed.
I just went to this site and it failed, http://www.lua.org/cgi-bin/demo
I used, print(math.random(1,2,0.1)) and it failed on both the app and the website.
Also, tostring doesn't matter :P. I'm assuming he wants the number, not the string. The print(' ') statement was probably just an example as it wouldn't be used in a real game.
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
print(math.random(10,20)/10) for tenths place or print(math.random(100,200)/100) to print the hundredths place.
https://scriptinghelpers.org/questions/30374/how-to-make-a-wait-where-the-wait-time-is-randomized-between-x-and-y#33721
I'm not sure why you're asking the same question twice
^ obviously Blue's answer is better than mine. I guess the simple answer is just use math.random() + 1 :P