Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

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... DiamondComplex 285 — 4y

2 answers

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago
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! :)

Ad
Log in to vote
0
Answered by 5 years ago

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

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. chomboghai 2044 — 5y

Answer this question