Monkuar Posted October 13, 2014 Share Posted October 13, 2014 (edited) Stupid Question, and it might even be as simple as adding a zero, but I cannot figure it out. $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,rand() LIMIT 1"); As you can see this works out perfectly. But only one issue. I want that 0.05 to be in the thousandths of percent: 0.005%. So it's a higher probability. But if I change it to 0.005, it never shows, even with iterations over 5000 in a for loop? I feel like I need to do something with my mt_rand? I need to simply move a decimal over someplace, but not sure where. Edited October 13, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291600-probability-by-the-thousandths-of-percent/ Share on other sites More sharing options...
requinix Posted October 13, 2014 Share Posted October 13, 2014 (edited) The only time when you'll get that 0.005 row is if $prob == 0 and that will never happen because the range on the random number is [0.05, 1]. Edited October 13, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/291600-probability-by-the-thousandths-of-percent/#findComment-1493430 Share on other sites More sharing options...
Monkuar Posted October 13, 2014 Author Share Posted October 13, 2014 (edited) The only time when you'll get that 0.005 row is if $prob == 0 and that will never happen because the range on the random number is [0.05, 1]. Ok. I increased the range to thousandths now: $prob = number_format(mt_rand(1,100)/100, 2) ; to: $prob = number_format(mt_rand(1,100)/100, 3) ; 2->3> So now when I'm iterating in my loop testing probability it shows: Which is now in the thousandths place, but even after 5000 + iterations in the loop, I still cannot get that 0.005% to spawn, just bad RNG luck or is the probability to high? Wait, Edit: I changed it to: $prob = number_format(mt_rand(1,1000)/1000, 3) ; And it looks like it's working now. Edited October 13, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/291600-probability-by-the-thousandths-of-percent/#findComment-1493431 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.