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

How do I use the Value Math.Random gives me?

Asked by 2 years ago

Okay so I want to use math.Random to give me a number between 1,2 so I did math.random(1, 2) So for example if I wanted the script to be like if Random == 1 then print("Hi") if Random == 2 then print("Hello") Ik this doesn't work you get the point

4 answers

Log in to vote
4
Answered by
appxritixn 2235 Moderation Voter Community Moderator
2 years ago

number math.random ( number m = 0.0, number n = 1.0 )

math.random returns a value between m and n. To store the value for later use, you would equate a variable to the result:

local RandomNumber = math.random(1,5) -- this will return a number between 1 and 5

To enhance the "randomness" of the numbers returned, you could use:

void math.randomseed ( number x )

math.randomseed(tick())

As for using the random number returned by math.random(m,n), that depends on how you are planning on using it. If you want to simply check what number was returned, you would use an 'if' statement:

local RandomNumber = math.random(1,5) 
if RandomNumber == 1 then
    -- Do something
elseif RandomNumber == 2 then
    -- Do something else
end
Ad
Log in to vote
0
Answered by 2 years ago
local randomNumber = math.random(1,2)
print(randomNumber)
0
I already know that austenman123 2 — 2y
0
I am trying to do something more complex with the generator austenman123 2 — 2y
Log in to vote
0
Answered by 2 years ago

I know that, but I am trying to do something more complicated as for example

math.random(1, 2) if math = 1 then game.workspace.sound.playing = true if math = 2 then game.workspace.sound2.playing = true something like that

Log in to vote
0
Answered by 2 years ago

So to do this math.random requires a minium and a highest number all need to be whole numbers to use this just put math.random(1, 100) or what ever you want where you want the number to be

0
Yeah I already did that, its 1-3. austenman123 2 — 2y

Answer this question