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?

Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

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.