Jump to content

Select an array based on probability value.


Monkuar
Go to solution Solved by Monkuar,

Recommended Posts

Here is my code:

//Grab all the monster in Monster zone 1

$q = $db->query('SELECT * from rpg_monsters where monster_zone = 1');

while ($monsterdata = $db->fetch_assoc($q))
    {
        $monster_chance[] = $monsterdata;
        
    }

and my var_dump:

array (size=4)
0 =>
array (size=
'monster_id' => string '1' (length=1)
'monster_name' => string 'Merman' (length=6)
'monster_level' => string '1' (length=1)
'monster_type' => string 'Normal' (length=6)
'monster_hp' => string '10' (length=2)
'monster_dmg' => string '1' (length=1)
'monster_zone' => string '1' (length=1)
'chance' => string '0.10' (length=4)
1 =>
array (size=
'monster_id' => string '2' (length=1)
'monster_name' => string 'Chris Wilson' (length=12)
'monster_level' => string '2' (length=1)
'monster_type' => string 'Unique' (length=6)
'monster_hp' => string '25' (length=2)
'monster_dmg' => string '3' (length=1)
'monster_zone' => string '1' (length=1)
'chance' => string '0.04' (length=4)
2 =>
array (size=
'monster_id' => string '3' (length=1)
'monster_name' => string 'Seaman Warrior' (length=14)
'monster_level' => string '2' (length=1)
'monster_type' => string 'Magic' (length=5)
'monster_hp' => string '20' (length=2)
'monster_dmg' => string '2' (length=1)
'monster_zone' => string '1' (length=1)
'chance' => string '0.10' (length=4)
3 =>
array (size=
'monster_id' => string '4' (length=1)
'monster_name' => string 'Goblin Attacker' (length=15)
'monster_level' => string '2' (length=1)
'monster_type' => string 'Normal' (length=6)
'monster_hp' => string '15' (length=2)
'monster_dmg' => string '2' (length=1)
'monster_zone' => string '1' (length=1)
'chance' => string '0.10' (length=4)

As you can see, the Unique Monster (Chris Wilson) is the lowest chance at only 4%. And all the other mobs at at 10% chance to spawn.

 

Here is my code for probability:

$rate = (double) '0.03'; // 3%
$max = 1 / $rate; // 100
if (mt_rand(0, $max) === 0) {
    // chance < $rate
    echo "Only 3% probability, holy cow!";
}

My problem is, I'm a bit lost as how to get my probability code inside a loop and having the dynamic $rate correspond to my "chance" row.

Although, I could just use ORDER BY rand() in mysql, it wouldn't let me use any probability and chances... hmmm

Edited by Monkuar
Link to comment
Share on other sites

Sorry, I don't understand what you are trying to do.

 

Okay, scratch a bit of my first post.

 

So, I have these values in my array (They are in the row "chance")

array (size=4)

0 => string '0.10' (length=4)

1 => string '0.04' (length=4)

2 => string '0.10' (length=4)

3 => string '0.10' (length=4)

Theoretically, the 10%'s will be spawned first over the 4% one right?

 

So, I'm trying to figure how to do that with:

$rate = (double) '0.03'; // 3%

$max = 1 / $rate; // 100

if (mt_rand(0, $max) === 0) {

    // chance < $rate

    echo "Only 3% probability, holy cow!";

}

I need to change that '0.03' to the values in my array: ( 0.10, 0.04, 0.10, 0.10 ) dynamically so I can get which array should be chosen if that makes sense?

 

 

I'm just trying to make monsters have different chances to spawn over other monsters. For a example, a unique monster should be at 0.04% (4%) to spawn, as compared to a Normal White monster 0.10 (10%). The unique needs to have a lower probability. 

 

I am a bit lost myself. 

Edited by Monkuar
Link to comment
Share on other sites

  • Solution
 
$rate = (double) '0.50'; // 3%
$max = 1 / $rate; // 100
if (mt_rand(0, $max) === 0) {
$uniquechance = true;
}



if ($uniquechance == true){
$q = $db->query('SELECT * from rpg_monsters where monster_zone = 1 AND monster_type="Unique" ');
}else{
$q = $db->query('SELECT * from rpg_monsters where monster_zone = 1');
}


while ($monsterdata = $db->fetch_assoc($q))
    {
    

            
            $monsterarray[] = $monsterdata;
        
    }
 

Boom, I need to keep this shit simple.. lol 

 

Sorry about that Barand, I was just over-thinking!

Edited by Monkuar
Link to comment
Share on other sites

I was thinking something like this

$prob = number_format(mt_rand(1,100)/100, 2) ;

$q = $db->query("SELECT * FROM rpg_monsters 
                WHERE monster_zone = 1 AND chance >= $prob
                ORDER BY chance
                LIMIT 1");

$monsterdata = $db->fetch_assoc($q);

So, in theory, if prob > 10  you get no monsters, 10% of the time you get a white one and 4% you get a unique one

Edited by Barand
  • Like 1
Link to comment
Share on other sites

I was thinking something like this

$prob = number_format(mt_rand(1,100)/100, 2) ;

$q = $db->query("SELECT * FROM rpg_monsters 
                WHERE monster_zone = 1 AND chance >= $prob
                ORDER BY chance
                LIMIT 1");

$monsterdata = $db->fetch_assoc($q);

So, in theory, if prob > 10  you get no monsters, 10% of the time you get a white one and 4% you get a unique one

 

This actually looks more unified and simpler... I'll try this out, thanks.

 

And to add even more probability to it (increase the odds), I could change the , 2) to ,3) to add a decimal place correct?

 

I'll only do that for really unique items though, not sure if I'll do it for just spawning monsters.

 

Edit: I even added it inside a for loop and changed the 0.10 to 1.00 except for Chris Wilson because it wasn't returning any data.

for ($x=0; $x<=100; $x++) {
 

$prob = number_format(mt_rand(1,100)/100, 2) ;
echo $prob;
$q = $db->query("SELECT * FROM rpg_monsters
                WHERE monster_zone = 1 AND chance >= $prob
                ORDER BY chance
                LIMIT 1");

$monsterdata = $db->fetch_assoc($q);
var_dump($monsterdata);

}

This is a good way to test probability too I assume? I get roughly 4% Chris Wilson each time.  And I changed the Goblin Attacker to 0.50 (50%) and he's showing up more than others.

 

This is awesome, love probability and the randomness.  And I'm just using that inside that for loop for testing, will never use that on production lol.

Edited by Monkuar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.