Raised This Month: $51 Target: $400
 12% 

Trigger function with % chance


Post New Thread Reply   
 
Thread Tools Display Modes
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 02-22-2017 , 14:17   Re: Trigger function with % chance
Reply With Quote #11

Quote:
Originally Posted by Airkish View Post
Should I replace this to Emp`'s code and what would be the difference?
Readability?
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-22-2017 , 23:26   Re: Trigger function with % chance
Reply With Quote #12

Quote:
Originally Posted by Emp` View Post
Code:
stock bool Chance( iNum, iMult=100 )
	return ( iNum * iMult >= random_num( 1, 100 * iMult ) );
Code:
if ( Chance( 5 ) )
{
   //...
}
iMult doesn't do anything in this function. The only thing it will do is break it if it's less than 1. I.e. if you remove it, it will work exactly the same.
__________________
fysiks is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 02-23-2017 , 00:24   Re: Trigger function with % chance
Reply With Quote #13

Code:
random_num(1, 101) <= 5

Code:
random(100) < 5
PartialCloning is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-23-2017 , 00:32   Re: Trigger function with % chance
Reply With Quote #14

Quote:
Originally Posted by PartialCloning View Post
Code:
random_num(1, 101) <= 5
This is wrong. This will give you 5/101, statistically of course. It is always important to check the boundary conditions. For example, if you want 100% instead of 5%, there is 1 possible value, 101, that will result in False.
__________________

Last edited by fysiks; 02-23-2017 at 00:38.
fysiks is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-23-2017 , 04:50   Re: Trigger function with % chance
Reply With Quote #15

Why not
PHP Code:
stock bool:Chance(Float:chance)
{
    return 
random_float(0.01.0) * 100.0 chance;
}

// Call
if(Chance(42.1337))
{
    
// 42.1337% chance

klippy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-23-2017 , 20:52   Re: Trigger function with % chance
Reply With Quote #16

Because the result is statistical, the higher resolution won't really change the outcome for any use case here.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:13.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode