Calculating a result based on a given percentage
by Jacob · in Technical Issues · 09/19/2005 (6:05 pm) · 6 replies
I am trying to figure out how to do this...
For example, I would like to determine the success of an action that the player attempts, based on some skills, which when eveluated will provide a "percentage chance of success". So I thought that I could just do the following:
Now this would work if %chance is something like 50, 10 or 2 but being 82, things don't work out as planned because %rand becomes a float.
Even if I rounded %rand to a whole number, the result wouldn't be correct/precise. Any ideas?
Edit: As you can see, getRandom will return 0 or 1, making the success of the action 50% and not 82% as desired.
If I do this:
I will always get a 1, making it 100% - also not what I want :)
For example, I would like to determine the success of an action that the player attempts, based on some skills, which when eveluated will provide a "percentage chance of success". So I thought that I could just do the following:
%chance = 82; // percent chance %rand = (100 / %chance); // 1.2195 %success = getRandom(%rand); if(%success == %rand) return true; // success! else return false;
Now this would work if %chance is something like 50, 10 or 2 but being 82, things don't work out as planned because %rand becomes a float.
Even if I rounded %rand to a whole number, the result wouldn't be correct/precise. Any ideas?
Edit: As you can see, getRandom will return 0 or 1, making the success of the action 50% and not 82% as desired.
If I do this:
%success = getRandom(1, %rand);
I will always get a 1, making it 100% - also not what I want :)
About the author
#2
but it should have been
I changed my first post - does my question make more sense now?
09/19/2005 (7:04 pm)
Edit: I see how you got that...I had %rand = 100 // %chance;
but it should have been
%rand = (100 / %chance);
I changed my first post - does my question make more sense now?
#3
Edit: spelling
09/20/2005 (5:13 am)
I figured out what Scott was saying now - all is well. Thank You!Edit: spelling
#4
09/20/2005 (6:37 am)
Maybe it can also be done in this way:%change = 82; %rand = getRandom(1, 100); if (%rand <= %change) return true; else return false;
#5
09/20/2005 (7:24 am)
Yes Manoel's method would work.
#6
09/20/2005 (7:40 am)
Thats what I figured too, it's interesting how these things come to me after I post a question :) - anyway, thanks!
Torque Owner Scott Casey