Jump to content

Getting a rand percentage using 1 number


gurpreet94

Recommended Posts

I have a form made to enter 1 number, and stored in the DB. On another page this gets called, and I need a way of turning that number into a percentage chance.

E.G I input 50, and that gets stored. On the page it's needed, how would I turn 50 into 50% chance (rand(1,2)), but it has to work for every number inputted. Any ideas?

// fetch $num from the db
$num = 50;
if(rand(1, 100) <= $num) {
    // event occured
}

 

Note that this will only work for integer percents, if you want support percentages like 50.35% you'll need to tweak it a bit.

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.