Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you get a random value between positive value and negative value?

How do you get a random value between a positive and a negative value using math.random. Like uh get a random value between -10 and 10.

1 comment

0

It's really easy; Idk why they don't use it; It can be done in a single line of code; Reply if you want...

2 answers

local rng = Random.new(tick()) -- generate a new random number generator
local randomNumber

-- if you want only integers (i.e. 1, 2, 3, -1, -2, -3, etc) then
randomNumber = rng:NextInteger(-10, 10)

-- if you want real numbers (i.e. 1.5, 3.14, 2.16112231, etc) then
randomNumber = rng:NextNumber(-10, 10)

Hope this helps! :)

To simplify this you can do:

local a = math.random(-10, 10)
print(a)

finding random numbers can be used in multible different ways here

1 comment

0

Actually math.random runs a different algorithm than the Random instance and can cause weird behavior such as: https://devforum.roblox.com/t/math-random-returns-same-number-every-time-after-a-delay-from-setting-randomseed/25243/14?u=zeuxcg . I submitted my answer using Random instead of math.random because simply put, it is better.

This discussion is preserved as it was. New answers, comments, and votes are closed.