LivingReceiver Posted January 23, 2013 Share Posted January 23, 2013 Hello Everyone! I was just wondering if anyone knew how to code a word generator that will give an three outputs without repeating any of the words. I want to make three radio buttons that have a random generated word from a list of words I created, but none of the words can repeat. I have the list of the words on a text file, and this is the code I am currently using. (All I did was repeat it three times to get the three outputs, but when I tested it I have had times where it repeats the same word three times.) <?php $filename="words.txt"; $words=file($filename); shuffle($words); $word=$words[0]; echo $word; ?> Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/273564-word-generator-that-doesnt-repeat/ Share on other sites More sharing options...
Psycho Posted January 23, 2013 Share Posted January 23, 2013 You almost have it. I'm guessing you are running that same block of code three times which is like picking a card out of a hat, putting the card back, and picking another card. You need to pick three words without starting over. After you shuffle all the words to randomize them you just need to pick the top three records. $filename="words.txt"; $words=file($filename); shuffle($words); $randomWords = array_slice($words, 0, 3); echo "<pre>" . print_r($randomWords, true) . "</pre>"; // Or echo $randomWords[0]; echo $randomWords[1]; echo $randomWords[2]; Quote Link to comment https://forums.phpfreaks.com/topic/273564-word-generator-that-doesnt-repeat/#findComment-1407825 Share on other sites More sharing options...
LivingReceiver Posted January 23, 2013 Author Share Posted January 23, 2013 You are an absolute lifesaver. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/273564-word-generator-that-doesnt-repeat/#findComment-1407833 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.