Jump to content

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.

Edited by Monkuar

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.

Edited by Monkuar
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.