gurpreet94 Posted October 31, 2011 Share Posted October 31, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250159-getting-a-rand-percentage-using-1-number/ Share on other sites More sharing options...
Alex Posted October 31, 2011 Share Posted October 31, 2011 // 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. Quote Link to comment https://forums.phpfreaks.com/topic/250159-getting-a-rand-percentage-using-1-number/#findComment-1283698 Share on other sites More sharing options...
gurpreet94 Posted October 31, 2011 Author Share Posted October 31, 2011 Thank you very much Alex, that's what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/250159-getting-a-rand-percentage-using-1-number/#findComment-1283703 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.