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

How do I set up a range of numbers?

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 :)

2 comments

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?

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

2 answers

✓ Accepted answer

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'

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 comment

1

You could also just use math.random(1, 100)....

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