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

What is the easiest way to Randomize randomizations?

Asked by 9 years ago

I tried using the or function, but that just returned the first set of numbers and not the second, so it always spawn in the same general area. Is there an easy way to do this, or do I need to create random values that dictate these ones...

local pos = Vector3.new((math.random(-150,-25)or math.random(25,150)),0,(math.random(-150,-25)or math.random(25,150)))

2 answers

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

You could make a table of possible choices, and retrieve them using a function whenever you need them.

local choices = {25, -25, 50, -50}

local function rand ()
    return choices[ math.random( #choices ) ]
end

print(rand(), 1, rand())
--Vector3.new(rand(), 0, rand())
0
Ahh, use tables, makes sense thank you BSIncorporated 640 — 9y
0
yup ImageLabel 1541 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

While ImageLabel's way is very correct, I am going to go into further detail, and create a table of random numbers.

So first,

let's start by creating a table that will hold all of our numbers.

numbers = {}

Great,

now we can setup a function that's going to generate our table. Additional detail will be given in the comments.

function GenerateTable()
    checkNumber = false -- We will set this to true if the number already exists. Won't happen commonly, but just in case.
    for i = 1, 50 do -- Generate 50(ish) random numbers
        for _,v in pairs(numbers) do
            local randomNumber = math.random(0,100) -- Our random number
            if randomNumber == v then
                checkNumber = true --Uh oh, already exists. This is where the 50(ish) comes into play. If this happens, it will only generate 49 (unless it happens multiple times. Very unlikely).
            end
        end

        if not checkNumber then
            table.insert(numbers, randomNumber)
        end
    end
end

Now

that we have a random set of numbers, about 50 of them, we can refer back to ImageLabel's answer for the rest.

function generateNumber()
    return numbers[math.random(1,#numbers)]
end
Note: This is all from general knowledge, none of this has been tested, and this is not the most efficient way. This is just a way to get you thinking.

Hope I helped.

0
I was considering something like that too but given that he was using vector3, i assumed it was going to be a very specific task, and that the numbers he provided would be crucial to whatever he was planning to do. Anyways, nice detailed explication . ImageLabel 1541 — 9y

Answer this question