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? 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. 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. 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
Archived
This topic is now archived and is closed to further replies.