Raised This Month: $ Target: $400
 0% 

how to make something have a 'chance'


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-15-2008 , 23:28   how to make something have a 'chance'
Reply With Quote #1

Sort-of new to scripting, I know a good bit though.
I'm looking to learn how to make things have a chance of happening, such as the RTD plugin. I checked out RTD source but it's pretty jam packed with stuff I'm not familiar with.
I'm hoping somebody nice would like to explain this to me ;)
-PvtSmith
PvtSmithFSSF is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-16-2008 , 00:00   Re: how to make something have a 'chance'
Reply With Quote #2

Just generate a random num and check for a constant value. Getting a number from 1 to 100,for example, and checking if it is 1,would give you a 1/100 or a 1% chance.

Use random_num() for this.
danielkza is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 08-16-2008 , 00:12   Re: how to make something have a 'chance'
Reply With Quote #3

I suggest random 0-10000 and check if less than 100 for 1% chance
or random float from 0 to 1 and check the percent directly
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-16-2008 , 00:22   Re: how to make something have a 'chance'
Reply With Quote #4

Quote:
Originally Posted by jim_yang View Post
I suggest random 0-10000 and check if less than 100 for 1% chance
or random float from 0 to 1 and check the percent directly
There's absolutely no difference. Any method will work. The chances are matematically the same. But it gets easier to read if you use numbers equal to the chance you are trying to achieve.

You could do,for example, random_num(1,400) and check if it's equal to one, or random_num(1,800) and check if its <= 2. Which one is easier to figure out?
danielkza is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-16-2008 , 00:23   Re: how to make something have a 'chance'
Reply With Quote #5

I like using this method:

Code:
new percent = 10; // 10 percent chance if( random(100) <= percent ) {     // random number beat the 10% chance } else {     // 10% chance failed. }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-16-2008 , 00:27   Re: how to make something have a 'chance'
Reply With Quote #6

Quote:
Originally Posted by Exolent[jNr] View Post
I like using this method:

Code:
new percent = 10; // 10 percent chance if( random(100) <= percent ) { // random number beat the 10% chance } else { // 10% chance failed. }
A suggestion, percent should be a const/static if this is called often. Or you can just replace percent for a literal.

But no need to worry if it's not in a loop, PreThink or a heavy used task.
danielkza is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 08-16-2008 , 01:43   Re: how to make something have a 'chance'
Reply With Quote #7

From PokeMod
Code:
 Chance(id)
	return ( PlayersLevel(id) > random_num(0, TotalLevels + 1) );
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-16-2008 , 10:45   Re: how to make something have a 'chance'
Reply With Quote #8

Quote:
Originally Posted by danielkza View Post
There's absolutely no difference. The chances are matematically the same.
If random_num() generated random numbers, you'd be right, but it doesn't and so you're likely not.
__________________
No support via PM.
Lee is offline
PvtSmithFSSF
Senior Member
Join Date: Jul 2008
Old 08-16-2008 , 12:28   Re: how to make something have a 'chance'
Reply With Quote #9

So using Exo's method, would this work?
Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>

#define PLUGIN "test"
#define VERSION "1.0"
#define AUTHOR "pvtsmithfssf    "


public plugin_init() 
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_clcmd("say gamble", "gamble")
}

public gamble(id)
{
    new percent = 50;  
    if( random(100) <= percent ) 
    {
        client_print(id, print_chat, "Congratulations! You've won low gravity!")
        cs_set_user_gravity(id, 0.5)
    } 
    else 
    {
        client_print(id, print_chat, "Sorry, you didn't win anything this time.")    
    }
}
Also, how would I make it so, like rtd, there's a 10% chance for THIS to happen, another 10% chance for THIS to happen, etc. So like there are 10 different things that can happen but only 1 happens, and they all have the same (10%) chance of happening.
PvtSmithFSSF is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 08-16-2008 , 13:04   Re: how to make something have a 'chance'
Reply With Quote #10

Quote:
Originally Posted by Lee View Post
If random_num() generated random numbers, you'd be right, but it doesn't and so you're likely not.
I think the level of randomness provided by the standard functions is good enough for 99% percent of the aplications.
danielkza 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 03:07.


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