This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
koslas's profile picture

Percentage

Started by koslas, 21 September 2015 - 12:45 PM
koslas #1
Posted 21 September 2015 - 02:45 PM
I'm working on a game, and I have it to mine something, that it works out your mining level, and what your mining, and will do math.random() to pick a number, between 1, and what it's worked out. I want to be-able to have it work out the percentage change of succesfully mining it


chance = miningRequiredLevel/miningLevel*30
randomNum = math.random(1,chance)
cPrint(randomNum.."/"..chance)

This just shows me the chance I have to mine it, with a mining level of 41, and the miningRequiredLevel at 30, chance = 21.95122, but I would like to know how to make that appear as a percentage, for another thing you can mine at level 1, with a mining level of 41 it's chance is 1, so that it's prints as "1/1" meaning a 100% success rate, I just don't understand percentages enough to be-able to make something appear as one, I did try Google it, but couldn't work it out, so any help would be much appreciated
KingofGamesYami #2
Posted 21 September 2015 - 03:10 PM
I'm confused as to what you want to do? A percentage is easy to calculate, it's just division and multiplication.


local percentage = current / maximum * 100

So if you have a max mining level of 10, and you have a current mining level of 1, percentage would equal 10%.
koslas #3
Posted 21 September 2015 - 03:24 PM
Well I have it with an experience system, each time it successfully mines, it adds the amount of experience for the thing you mined, you get a certain amount of experience, and then when you reach a certain amount, your level goes up. There isn't really a max mining level you can get (It's capped at 50 atm, by slowly getting more experience) but it does

chance = miningRequiredLevel/miningLevel*25
MiningRequiredLevel = 15 for this ore, but the miningLevel will change as you level it up. It then does math.random(1,chance) and sees if it = 1 then, so I want to work out the percentage of what the chance of you getting a 1 is from math.random()
Sorry about confusing you, as you might be-able to tell, I'm not the best at explaining things very well
KingofGamesYami #4
Posted 21 September 2015 - 03:45 PM
So you have:


math.random( 1, miningRequiredLevel/miningLevel * 25 )

Which gives you a number between 1 and miningRequiredLevel/miningLevel * 25. You want the percentage of the time it will equal 1, which is


local percentage = 1 / (miningRequiredLevel / miningLevel * 25 )
Edited on 21 September 2015 - 01:45 PM