iStriide Posted August 6, 2011 Share Posted August 6, 2011 I'm wanting to know if there is a random function in PHP. I am not sure if there is one, but I could I do something where I can make something a have lets say a 25% chance of happening. Example: Can I do anything like this? 25% chance of echoing "Whats Up" or 75% chance of echoing "I hate lag". If there is anything like this any PHP I would appreciate if you guys would tell me how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/ Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 $variable = array("whats up","whats up","I hate lag","I hate lag","I hate lag"); shuffle($variable); print $variable[0]; thats all i can think of at the moment, my brain is fuzy Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253084 Share on other sites More sharing options...
iStriide Posted August 6, 2011 Author Share Posted August 6, 2011 I just tried that out, and it seems like it just goes in order. Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253087 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 It uses Shuffle, i tried it, the percentage of I hate lag was much higher than whats up, Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253088 Share on other sites More sharing options...
iStriide Posted August 6, 2011 Author Share Posted August 6, 2011 Yea I just tested it out a bit more and it seems that way to. I appreciate the help. This should work for what i'm trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253089 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 <?php $variable = array("whats up","whats up","I hate lag","I hate lag","I hate lag"); $p1 = 0; $p2 = 0; for($i=0;$i<=100;$i++){ shuffle($variable); if($variable[0] == "whats up"){ $p1++; } else { $p2++; } } print $p1 . '<br>'; print $p2; ?> 34 - Whats up 67 - I hate Lag 25- Whats up 76- I hate Lag 37- Whats up 64 - I hate Lag Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253090 Share on other sites More sharing options...
MasterACE14 Posted August 6, 2011 Share Posted August 6, 2011 $arr = array('whats up', 'I hate lag'); $rand = rand(1,25); // 25% chance max $remainder = 100 - $rand; echo $arr[0].' : '.$rand.'%<br />'; echo $arr[1].' : '.$remainder.'%<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253113 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 edit: MasterACE14, I like you method. Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253115 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 $arr = array('whats up', 'I hate lag'); $rand = rand(1,25); // 25% chance max $remainder = 100 - $rand; echo $arr[0].' : '.$rand.'%<br />'; echo $arr[1].' : '.$remainder.'%<br />'; I would just to point out that this is not valid. Because Whats up and I Hate lag each half a 50/50 chance of happening in reality, while your only producing percentages not based on the array. OP's question wasn't very clear, because you would need to actually check how many times whats up and i hate lag occur based on the array elements. Just a heads up. I like your method though Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253119 Share on other sites More sharing options...
voip03 Posted August 6, 2011 Share Posted August 6, 2011 phpSensei Small correction In your method the total is 101, but if you change the “for loop” counting up to 99 then the total will be 100. for($i=0;$i<=99;$i++){ shuffle($variable); if($variable[0] == "whats up"){ $p1++; } else { $p2++; } } Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253162 Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 i was watching 101 dolmations (sp) Quote Link to comment https://forums.phpfreaks.com/topic/244010-is-there-anything-like-a-random-function/#findComment-1253164 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.