Selecting Value from Table

:
How would I go about selecting one of the values from this table so that some values are selected more commonly than others.

I’m not sure how to do this with my current knowledge.
I can’t find anything helpful.

If you are talking about picking at random chance, take a look at this: Spinning crate system - #4 by Affenity

Technically the more instances of x you add to an array, the chance for getting x increases, as x/n (the probability of getting it) increases (with x/n ^ t decreasing, the probability of getting it again in a row, t times) ,

For instance if you want more chances of getting “Orc” from the array , you could add more instances of “Orc” in the array,
like :

{“goblin”,“goblin”,“goblin”,“goblin”,“human”}
would cause “goblin” to technically be the most common, but it depends on how you exactly select a value.

You could even implement some sort of weightage mechanism, a form described below:

I created this weightage script based on math.random and NumberRanges for a simple methodology, though there are several other methods of setting weightages for random values etc. , potentially even better.

Minimum = 1--  set a minimum value, so the random value falls in between a range of the Minimum and Maximum.
Maximum = 700 -- set this to your maximum possible value , from the ranges in the container

local container = {

        Goblin = NumberRange.new(1,5);   -- Very Rare
        Human  = NumberRange.new(5,15);
        Apple  = NumberRange.new(15,30);
        Orc    = NumberRange.new(30,60);
        Orange = NumberRange.new(60,250); --Very common
        Seed   = NumberRange.new(250,700) -- SUPER Common

}

local rand = math.random(Minimum,Maximum)

for item, chance in pairs(container) do
    if rand >= chance.Min and rand <= chance.Max  then
        Reward = item
        print(Reward)
    end
end

Set the minimum and maximum values according to the bounds of the number ranges in the “container” table, the more the range (or difference between highest and lowest values) is for any range in the dictionary, the more the chances for the random value falling in a certain range, essentially being the most common, opposite for the least common value, where the range is spread over the least amount of values.

Keeping an item where the minimum limit falls under another range’s maximum value would result in two items to be selected at one time, where the random value fell between 2 ranges.

Again, there might be several other methods, as @KeysOfFate has linked, which you can utilize, but I gave this methodology because it gets the job done, regardless of efficiency.

5 Likes

Well for one if you’re going to do this, math.random will become a great friend.
I recommend for chances increasing your table then though. So if you want orc to be more popular, spam orc in the table more often. It’s all about probability, at least that’s what I’m thinking.

So if orc shows up more, the probability of getting orc will be higher. This is a great example of why math class is important. :+1:

So maybe if you want orc to be more common, your table looks like this:

local Races = {"Orc", "Orgre", "Orc", "Human", "Orc", "Orc", "Elf", "Orc", "Dwarf", "Orc", "Orc"}
-- and on and on in a somewhat random order but placed specifically to increase the chances.

Then its as simple as using

local x = 6 -- This would actually be how big your table is
math.randomseed(math.random(tick())) -- I like to use this before calling math.random() as it will make it more random in my opinion. Will also lead to varied results in game.
local result = math.random(x) 
-- and call it
local selectedRace = Race[result]

Hope that might help you out a bit?

1 Like

RoStrap has a weighted probability function you can grab:

https://raw.githubusercontent.com/RoStrap/Math/master/WeightedProbabilityFunction.lua
(It uses other RoStrap functions but you could modify it to not to)

1 Like

Thank you so much, this is extremely helpful to me.

1 Like

@blacksuspect i have a module that handles this
it uses a method similar to what @XxELECTROFUSIONxX used
https://www.roblox.com/library/4878990888/LootTable

Example usage

--require the LootTable module
local rt = require(script.ServerScriptService.LootTable)

--assign weights to certain tags. (common,uncommon,rare etc)
local weights = {Common = 100, Uncommon = 50, Rare = 5, Epic = 3, Legendary = 1}

--Roll the weights table and see what you get!
local tag = rt:Roll(weights)


--make something happen when you land on a certain loot tag.
if(tag == "Common") then
    print("You landed on a common tag!") 
    --give player a common item here
end
Click to see full print results
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Rare
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Legendary
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Rare
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Epic
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Rare
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Legendary
CLICK
  You rolled a Common
CLICK
  You rolled a Rare
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Rare
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Epic
CLICK
  You rolled a Epic
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Rare
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Uncommon
CLICK
  You rolled a Common
CLICK
  You rolled a Common
CLICK
  You rolled a Rare
CLICK
  You rolled a Common
1 Like