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

how to randomly select numbers?

Asked by 4 years ago

how to select a random number in a range of numbers ie selecting 3 and 7 from a list of 1 to 10 instead of selecting 1,2,3 or 1,2,3,4,5,6,7?

4 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

Selecting a number, you can use math.random(min, max).

If you want to select a number that's 3 to 7, then do math.random(3,7) in your command bar, and you can see the output will show a number that's randomly generated in 3 to 7.

If you want to make something like randomly generate specific numbers like randomly selecting like 1,4,6,8,9, then you need to use a table and randomly pick a value inside the table by doing like this:

local table1 = {1,4,6,8,9}
local randomNumber = table1[math.random(#table1)]

print(randomNumber)

Check out your output!

If this was correct, mark this as the correct answer, thanks c:

Ad
Log in to vote
0
Answered by 4 years ago

math.random() https://developer.roblox.com/en-us/api-reference/lua-docs/math

Log in to vote
0
Answered by 4 years ago

You need to use a table

local table1 = {1,2,3,4,5,6,7,8,9,10}
local randomNumber = table1[math.random(#table1)]

print(randomNumber)
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Don't make it harder than it is.

local MyNumber = math.random(1,10)
print (MyNumber)

Done.

Answer this question