ladyath Posted July 21, 2010 Share Posted July 21, 2010 Not sure if anyone can help me with something like this. I am looking for a php script that can give me the following: Webpage with a dropdown listing the numbers 1 to 5. Depending on what number is chosen, to execute a script to give me a set of results. The results would be drawn from 5 tables. In the tables there would be an item listed with a category flag of red, blue, green or yellow. For example: If the rating is 2: * display 4 random results from table 1 where the category is red * display 2 random result from table 2 where the category is red * 20% chance to display a result from the blue category in table 1 or two. If the rating is 3: * display 5 random results from table 1 where the category is red * display 3 random result from table 2 where the category is red * display 1 random result from table 3 where the category is red * 20% chance to display a result from the blue category in table 1 or two * 5% chance to display a result from the green category in table 1 or two How can this be done? Sorry, but my knowledge on PHP is dismal at best Quote Link to comment https://forums.phpfreaks.com/topic/208384-complete-newbie-probability-based-on-an-argument/ Share on other sites More sharing options...
Daniel0 Posted July 21, 2010 Share Posted July 21, 2010 A 20% probability means that you have a 20/100 probability of that happening. You can just pick a random number like mt_rand(1, 100) and declare 20 of them the "positive" choice. Assuming that the output of PHP's PRNG is uniformly distributed, you can simply just choose the lower 20. Thus, you should be able to do it like this: function iCantThinkOfAGoodNameForThisFunction($probability) { return mt_rand(1, 100) <= $probability; } Then there should be a 20% probability that iCantThinkOfAGoodNameForThisFunction(20) returns true. Quote Link to comment https://forums.phpfreaks.com/topic/208384-complete-newbie-probability-based-on-an-argument/#findComment-1088981 Share on other sites More sharing options...
ladyath Posted July 21, 2010 Author Share Posted July 21, 2010 Thank you for the response, but how would I have all 5 criteria? Would it use a switch or an if statement? I am a bit lost on how to approach it. Quote Link to comment https://forums.phpfreaks.com/topic/208384-complete-newbie-probability-based-on-an-argument/#findComment-1089041 Share on other sites More sharing options...
Jessica Posted July 27, 2010 Share Posted July 27, 2010 Thank you for the response, but how would I have all 5 criteria? Would it use a switch or an if statement? I am a bit lost on how to approach it. If I understand your desired result, I'd use a switch. Can you elaborate on what you're doing or display some code? Quote Link to comment https://forums.phpfreaks.com/topic/208384-complete-newbie-probability-based-on-an-argument/#findComment-1091688 Share on other sites More sharing options...
smither07 Posted August 3, 2010 Share Posted August 3, 2010 A 20% probability means that you have a 20/100 probability of that happening. You can just pick a random number like mt_rand(1, 100) and declare 20 of them the "positive" choice. Assuming that the output of PHP's PRNG is uniformly distributed, you can simply just choose the lower 20. Thus, you should be able to do it like this: function iCantThinkOfAGoodNameForThisFunction($probability) { return mt_rand(1, 100) <= $probability; } Then there should be a 20% probability that iCantThinkOfAGoodNameForThisFunction(20) returns true. It helped me a lot, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208384-complete-newbie-probability-based-on-an-argument/#findComment-1094620 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.