• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved How to randomize several different values?

Ahilphino

Excellent OT User
Joined
Jun 5, 2013
Messages
1,667
Solutions
1
Reaction score
726
Ok, so I would like to randomize 4 different values and I want all of them to be different values, right now I use this:
Code:
local a, b, c, d = math.random(1,10), math.random(1,10), math.random(1,10), math.random(1,10)
but with this system I would for instance sometimes get 2 or more of the same number, how can I make it so that they're always different numbers and all of them roll from 1 to 10?
 
didn't quite understand that, even after trying several times reading it over and over and thinking about it xd
i'm really new to coding so, would appreciate something more detailed xD
 
You can use an array to store the random, and check if the value already exists there something like:
PHP:
$numbers = array();
while(count($numbers)<5){
  $rand = math.random(0,10);
  if(!in_array($rand,$numbers){
    array_push($rand,$numbers);
  }
}

EDIT::I think you are talking about lua. But can be easily converted, i think lua have array fnctions too
 
Last edited:
Code:
local numbers = {}
while (#numbers ~= 5) do
   local rand = math.random(1, 10)
   if (not table.find(numbers,  rand)) then
     table.insert(numbers, rand)
   end
end
to get the numbers, simply access the table by index, as shown:
numbers[1] would be the first random number, numbers[2] the second and so on..
 
Ok, so I would like to randomize 4 different values and I want all of them to be different values, right now I use this:
Code:
local a, b, c, d = math.random(1,10), math.random(1,10), math.random(1,10), math.random(1,10)
but with this system I would for instance sometimes get 2 or more of the same number, how can I make it so that they're always different numbers and all of them roll from 1 to 10?
and now lets make it super easy for you to constantly use that function.

Code:
local a, b, c, d = generateRandomNumbers(1, 10, 4)

-- first value is minimum value, second value is maximum value, third value is how many times cycles same function.
function generateRandomNumbers(minV, maxV, times)
if type(minV) ~= "number" then
    if type(minV) == "string" then minV = tonumber(minV)
    else print("variable times is: "..type(minV).." IN generateRandomNumbers()") return false
    end
end
if type(maxV) ~= "number" then
    if type(maxV) == "string" then maxV = tonumber(maxV)
    else print("variable times is: "..type(maxV).." IN generateRandomNumbers()") return false
    end
end
if times then
    if type(times) ~= "number" then
        if type(times) == "string" then times= tonumber(times)
        else print("variable times is: "..type(times).." IN generateRandomNumbers()") return false
        end
    end
else times = 1
end
if times > maxV then print("eh? wana crash or what? IN generateRandomNumbers()") return false end

local numbers = {}

    while (#numbers < times) do
        local randomN = math.random(minV, maxV)

        if not table.find(numbers, randomN) then
            table.insert(numbers, randomN )
        end
    end
   if #numbers > 7 then print("need more return values IN generateRandomNumbers()") end
return numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7]  -- its alright if its nil  value. I just don't know how to make return dynamical xD
end
Its not tested or anything, but should work.
Put that function where you want.
 
Last edited:
and now lets make it super easy for you to constantly use that function.

Code:
local a, b, c, d = generateRandomNumbers(1, 10, 4)

-- first value is minimum value, second value is maximum value, third value is how many times cycles same function.
function generateRandomNumbers(minV, maxV, times)
if type(minV) ~= "number" then
    if type(minV) == "string" then minV = tonumber(minV)
    else print("variable times is: "..type(minV).." IN generateRandomNumbers()") return false
    end
end
if type(maxV) ~= "number" then
    if type(maxV) == "string" then maxV = tonumber(maxV)
    else print("variable times is: "..type(maxV).." IN generateRandomNumbers()") return false
    end
end
if times then
    if type(times) ~= "number" then
        if type(times) == "string" then times= tonumber(times)
        else print("variable times is: "..type(times).." IN generateRandomNumbers()") return false
        end
    end
else times = 1
end
if times > maxV then print("eh? wana crash or what? IN generateRandomNumbers()") return false end

local numbers = {}

    while (#numbers < times) do
        local randomN = math.random(minV, maxV)

        if not table.find(numbers, randomN) then
            table.insert(numbers, randomN )
        end
    end
   if #numbers > 7 then print("need more return values IN generateRandomNumbers()") end
return numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7]  -- its alright if its nil  value. I just don't know how to make return dynamical xD
end
Its not tested or anything, but should work.
Put that function where you want.
Not bad, but i dont think this kind of funct is needed. Btw, i loved the return xd
 
thanks a lot for all of the replies, but I suspect this would take quite a bit of memory? For example my idea was to use this for a spell with a 1sec cooldown, but I guess that would take a lot of memory compared to more regular spells?
 
thanks a lot for all of the replies, but I suspect this would take quite a bit of memory? For example my idea was to use this for a spell with a 1sec cooldown, but I guess that would take a lot of memory compared to more regular spells?
compared to regular spells? spell takes ~1 tennis ball of memory, this function takes ~1 dust spec of memory.
Btw, you have total of Football arena memory left.

Actually got no clue, but trust me, this is practically nothing.
My functions generate tables what run trough multible 3 dimensional loops and I still don't feel lag with my 2gb memory.

Not bad, but i dont think this kind of funct is needed. Btw, i loved the return xd
You are prolly right, but though maybe its something Ahilphino will learn from and when he finaly does want to make something what he is going to use in multible places, then that would be good practice.
 
and now lets make it super easy for you to constantly use that function.

Code:
local a, b, c, d = generateRandomNumbers(1, 10, 4)

-- first value is minimum value, second value is maximum value, third value is how many times cycles same function.
function generateRandomNumbers(minV, maxV, times)
if type(minV) ~= "number" then
    if type(minV) == "string" then minV = tonumber(minV)
    else print("variable times is: "..type(minV).." IN generateRandomNumbers()") return false
    end
end
if type(maxV) ~= "number" then
    if type(maxV) == "string" then maxV = tonumber(maxV)
    else print("variable times is: "..type(maxV).." IN generateRandomNumbers()") return false
    end
end
if times then
    if type(times) ~= "number" then
        if type(times) == "string" then times= tonumber(times)
        else print("variable times is: "..type(times).." IN generateRandomNumbers()") return false
        end
    end
else times = 1
end
if times > maxV then print("eh? wana crash or what? IN generateRandomNumbers()") return false end

local numbers = {}

    while (#numbers < times) do
        local randomN = math.random(minV, maxV)

        if not table.find(numbers, randomN) then
            table.insert(numbers, randomN )
        end
    end
   if #numbers > 7 then print("need more return values IN generateRandomNumbers()") end
return numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7]  -- its alright if its nil  value. I just don't know how to make return dynamical xD
end
Its not tested or anything, but should work.
Put that function where you want.

Why not just return numbers? Completely inefficient to return each individual element. What if there's 3000 of them? dear god. If you return a table, that table can be accessed and manipulated all the same.
Code:
function functionThing(p1, p2, p3, p4)
local t = {}
blablabla
return t
end
local t = functionThing(x, y, z, bla)
--t is a table containing the return value of functionThing
Also, your type check has a fault in it.
It checks if it's a number. If not, checks if it's a string. If it is, it changes it to a number. That works great if the string is "1" but if the string is "cat" it will return nil and when you use
Code:
if times > maxV then print("eh? wana crash or what? IN generateRandomNumbers()") return false end
It will try to compare with a nil value. End of script by error.
 
What are you talking about?
I made this function for variables.
You can use this function in loop if you want thousands values.

And function has 3 parameters, where you taking 4th one?

Well, that error is good enough for user to understand something is fucked up.
False either way,
 
What are you talking about?
I made this function for variables.
You can use this function in loop if you want thousands values.

And function has 3 parameters, where you taking 4th one?

Well, that error is good enough for user to understand something is fucked up.
False either way,

if you wanted to set it to a lot more variables, like 3000 (example) you would need 3000 individual return values. Hence why you want to return a table, not individual elements or indices of a table.

The 4 parameter function was an example of how to return a table value, it has absolutely nothing to do with the existing script

And it will not return false either way. If it's a number, it can return false. If it's not a number, tt will return a lua error stating that you've tried to compare a nil value. It will not print your pre-made error message. That is usually not enough for a user to understand what's wrong. If it did, the support boards would be pretty empty.
 
if you wanted to set it to a lot more variables, like 3000 (example) you would need 3000 individual return values. Hence why you want to return a table, not individual elements or indices of a table.

The 4 parameter function was an example of how to return a table value, it has absolutely nothing to do with the existing script

And it will not return false either way. If it's a number, it can return false. If it's not a number, tt will return a lua error stating that you've tried to compare a nil value. It will not print your pre-made error message. That is usually not enough for a user to understand what's wrong. If it did, the support boards would be pretty empty.
ah i get it xD, yea, but well good enuf for me.

But I still don't understand what you are trying to imply with 3000 individuals.
This function is made for realistic use and just for current purpose and similar ones, where user is making useable variables.
Why would anyone want to make 3000 invidual random values in which all are different and all in use?
 
ah i get it xD, yea, but well good enuf for me.

But I still don't understand what you are trying to imply with 3000 individuals.
This function is made for realistic use and just for current purpose and similar ones, where user is making useable variables.
Why would anyone want to make 3000 invidual random values in which all are different and all in use?
As I said. It was just an example. Even if it was 20 or something, it's a lot of typing just to return the values. I'm just saying its best to return a table and access it later with a for loop rather than returning the individual values
 
Back
Top