Monkuar Posted October 12, 2014 Share Posted October 12, 2014 (edited) 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 October 12, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/ Share on other sites More sharing options...
Barand Posted October 12, 2014 Share Posted October 12, 2014 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. Sorry, I don't understand what you are trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/#findComment-1493402 Share on other sites More sharing options...
Monkuar Posted October 12, 2014 Author Share Posted October 12, 2014 (edited) 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 October 12, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/#findComment-1493403 Share on other sites More sharing options...
Solution Monkuar Posted October 12, 2014 Author Solution Share Posted October 12, 2014 (edited) $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 October 12, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/#findComment-1493404 Share on other sites More sharing options...
Barand Posted October 12, 2014 Share Posted October 12, 2014 (edited) 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 October 12, 2014 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/#findComment-1493410 Share on other sites More sharing options...
Monkuar Posted October 12, 2014 Author Share Posted October 12, 2014 (edited) 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 October 12, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291592-select-an-array-based-on-probability-value/#findComment-1493425 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.