AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how to make something have a 'chance' (https://forums.alliedmods.net/showthread.php?t=75964)

PvtSmithFSSF 08-15-2008 23:28

how to make something have a 'chance'
 
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

danielkza 08-16-2008 00:00

Re: how to make something have a 'chance'
 
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.

jim_yang 08-16-2008 00:12

Re: how to make something have a 'chance'
 
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

danielkza 08-16-2008 00:22

Re: how to make something have a 'chance'
 
Quote:

Originally Posted by jim_yang (Post 670344)
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?

Exolent[jNr] 08-16-2008 00:23

Re: how to make something have a 'chance'
 
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. }

danielkza 08-16-2008 00:27

Re: how to make something have a 'chance'
 
Quote:

Originally Posted by Exolent[jNr] (Post 670352)
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.

Emp` 08-16-2008 01:43

Re: how to make something have a 'chance'
 
From PokeMod
Code:

Chance(id)
        return ( PlayersLevel(id) > random_num(0, TotalLevels + 1) );


Lee 08-16-2008 10:45

Re: how to make something have a 'chance'
 
Quote:

Originally Posted by danielkza (Post 670351)
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.

PvtSmithFSSF 08-16-2008 12:28

Re: how to make something have a 'chance'
 
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.

danielkza 08-16-2008 13:04

Re: how to make something have a 'chance'
 
Quote:

Originally Posted by Lee (Post 670550)
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.


All times are GMT -4. The time now is 03:07.

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