Jump to content

Probability by the thousandths of percent


Monkuar

Recommended Posts

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");

d43b3cbd92da0d0dd41a1adea082aee3.png

 

 

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.

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:

 

899ab25144b122e3434577befb270e7b.png

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.