phpnoobie9 Posted March 27, 2008 Share Posted March 27, 2008 I have a loop showing random words by using order by rand(). I have 2 random words. How am I able to make it so rand() will give each of the word a 50% chance of showing? ....so if I refresh the browser twice I should of seen both of the words. Link to comment https://forums.phpfreaks.com/topic/98187-php-order-by-rand/ Share on other sites More sharing options...
cunoodle2 Posted March 27, 2008 Share Posted March 27, 2008 How about this... <?php $wordlist = array("hello","world"); echo $wordlist[rand(0,1)]; ?> Try that out. That should do the trick (I think) Link to comment https://forums.phpfreaks.com/topic/98187-php-order-by-rand/#findComment-502364 Share on other sites More sharing options...
phpnoobie9 Posted March 27, 2008 Author Share Posted March 27, 2008 What does 0,1 mean after rand? Link to comment https://forums.phpfreaks.com/topic/98187-php-order-by-rand/#findComment-502366 Share on other sites More sharing options...
soycharliente Posted March 27, 2008 Share Posted March 27, 2008 It will already have about a 50% chance of showing because there are only 2 choices. Choosing something randomly doesn't necessarily mean that you'll get a different one of the 2 words each time. You could get one word 3 times in a row then the other one 5 times in a row. If you really want to show the other word each time the page loads, set some flag on the words and change it between the two each time. rand(0,1) means pick a random number between 0 and 1. Link to comment https://forums.phpfreaks.com/topic/98187-php-order-by-rand/#findComment-502368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.