I want to know how to get like 12% of 234243 for example but
print(12 % 234243)
does not work
I want to know how to get like 12% of 234243 for example but
print(12 % 234243)
does not work
%
is the modulus operator. You wouldn’t need that for this problem. To get 12% of a number you can just multiply it by 0.12
print(234243 * 0.12)
So if I had a table like this?
local Table = {
Low = {
Object = 1
},
Medium = {
Object2 = 2
},
High = {
Object3 = 3
}
}
How would I make it a 25 percent chance to pick low object value?
How would I make it a 50 percent chance to pick medium object value?
How would I make it a 75 percent chance to pick high object value?
As @Blokav said you need to use the multiplier operator “*” not the modulus operator. To get the percentage of a number you need to multiply it by a decimal point between .01 or 1% or .99 or 99%.
I dont really understand what does modul=us even mean?
Modulus is to find the remainder of a number being divided by another number. For example, if I wanted to find all the numbers with positive square roots I would do “n % 2 == 0” Then I would print “n”. It can be more complex than that but if you’re using it for basic operations then that’s how you would most likely use it.
Not sure what you mean, but for random chances use math.random. Math.random picks a random number from x-y (x and y are self set variables - you get to choose what number they are).
Like @Paradigmed mentioned, the % sign in Lua and many other languages does not represent percentage.
So, you could do something like this:
local valueLanded = math.random(1, 150)
if valueLanded <= 25 then
-- Pick Low
elseif valueLanded > 25 and <= 100 then
-- Pick Medium
elseif valueLanded > 100 and <= 150 then
-- Pick High
end
You can think of math.random(1, 150)
as the values representing percentages. Now, you said that you want it there to be 25% chance, 50% chance, and 75% chance, but that’s impossible with a typical 0% - 100% range. You would have to scale down all of those percentages to where it adds up to 100. Currently, when you add 25, 50, and 75 percent the range becomes 0% - 150%, which is why I made it that way in the code.
So if you’d like to do it the more “correct looking” way, then simply change your chances to add up to 100%, and then chance the code accordingly.
Hope I helped!
Here is some code I wrote for someone else for something similar. This picks a value from the table based on the percent value it has. Obviously, you should probably rename some stuff.
local pets = {
["Dog"] = 25.5,
["Cat"] = 24.5,
["Bird"] = 50
}
local function pickAPet()
--Picks a number
local num = (math.random(0,10000)) / 100
--Variable Used in Loop
local sum = 0
--Loop runs though the pets and their percents
for pet, percent in pairs(pets) do
--Adds the current pet's percent to the sum
sum = sum + percent
--If the num is less than the sum the num was in the range of the percent
if num < sum then
--returns the pet(in string(word) form). Ex. math.random returns a number.
return pet
end
end
--If your percents add to less than 100 there is a chance no pet will be found. Can be set to a default pet just in case(ex. "Dog")
return "nopet"
end
This function returns the key name of the table element randomly based on the value the table element has.
this worked well ngl thank you!