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

How do I set up a range of numbers?

Asked by 8 years ago

I would like to make something where I could set up a set of values like

local numbers = {"1", "2",} --etc.
local pickednumber = numbers[math.random(1, #numbers)]

But I want to make it so I don't have to list every number to be able to pick from it.

If you have further questions or can help it would be appreciated :)

0
I don't understand what you want. Do you just want to get a random integer from 1 to n? Can you give a larger example so that it's clear? BlueTaslem 18071 — 8y
0
Instead of having to write all the numbers I want to choose from, I would like to be able to set up something that I could have, say, a range from 1-100 and I could pick a number from 1-100 randomly jimborimbo 94 — 8y

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

What You Need To Do

So, you want to randomly select a number, from a range of numbers that you specify?

You're really over-analyzing this, all you have to do is use the math.random function, with the second argument set as your range.


Code

local range = 100 --Define your range

local number = math.random(1,range) --Random number between 1 and 'range'
Ad
Log in to vote
0
Answered by 8 years ago

I'm not quite sure what your main goal is. But if you are looking to pick a random number here. This is on a loop, so fix it accordingly to what you would like it to be. Also note that the wait(1) is optional, so if you want the numbers to load slower, use it.

Code:

local numbers = {"0",}

for i = 1, 100  do
    --wait(1)
    table.insert(numbers,(i))
end

while wait(1) do
    local pickednumber = numbers[math.random(1, #numbers)]
    print(pickednumber)
end

Hope this helped!

1
You could also just use `math.random(1, 100)`.... BlueTaslem 18071 — 8y

Answer this question