yakoup46 Posted April 21, 2009 Share Posted April 21, 2009 So this is the code i have... $number = range('a','z'); $test = ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]); So if were to echo $test i would get four random letters. What i want to do is have it continue to generate random letters until it creates the word 'food'. I have absolutely no idea where to go from here. So any help would be awesome. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/ Share on other sites More sharing options...
Philip Posted April 21, 2009 Share Posted April 21, 2009 You could put it in a for loop... but you could be waiting a while. After all that's only 456,976 possible combinations of 4 letter words Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815175 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 you mean like a while() type deal? I thought about that but i was not really sure how to go about it. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815178 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 That sure i alot of combinations. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815179 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 do you have a list of words somewhere, that you want to check against? otherwise, maybe you can do something with pspell? Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815185 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 I could create a list of words. I was hoping that I could just test for a certain word right in the script. Kinda like: if($test = food) echo "whatever" Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815188 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 Is there any point to this? Maybe I/we could be of better assistance... Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815191 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 Yeah. I am in a statistics course for college and and we have to collect some form of data. So I was going to create a random word generator at see how long it takes each time to generate a word. Not necessarily four letter word cuz that could take forever but... and i was hoping to learn some PHP techniques at the same time. But if this is not practical, then i am sorry for having wasted everyone time. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815193 Share on other sites More sharing options...
WolfRage Posted April 21, 2009 Share Posted April 21, 2009 This maty not be exactly what you want but it will help. Also code below not tested. <?php $start=microtime(); $number = range('a','z'); $test = ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]); while($test!=='food'){ $test = ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]) . ($number[rand(0,25)]); } $finished=microtime(); echo 'This script started at: '.$start.' & finished at: '.$finished.' | The word food was found.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815199 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 well... The following works for me, its kinda cool, give it a try: <?php $wordList = array('cats','dogs','frog','mice','fart','mart','kart'); $str = 'abcdefghijklmnopqrstuvwxyz'; $good = FALSE; $c = 1; while(!$good){ $st = ''; for($i=0;$i<4;$i++){ $st .= $str{rand(0,25)}; } if(in_array($st,$wordList)){ echo $c.'. Found: '.$st; break; }else{ echo $c.'. Couldn\'t find: '.$st.'<br>'; } $c++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815207 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 O wow thank you very much. I will look at that more intensely so that i can really understand what's going on, but that was exactly what i was looking for, unfortunately i was totally of base in my script. But yours will serve as a very good tool. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815211 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 Wow you are fascinating at PHP i have absolutely no idea how your script is working. Hopefully i can figure it out. LOL. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815214 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 I commented everything for you, let me know if you need any more explanation: <?php // Array of words we will check for $wordList = array('cats','dogs','frog','mice','fart','mart','kart'); // A string list of letters $str = 'abcdefghijklmnopqrstuvwxyz'; // Set to true, so we have an endless while loop $good = FALSE; // Set the counter at one $c = 1; // Start the main loop while(!$good){ // Create an empty string $st = ''; // Create a loop to make a four digit string for($i=0;$i<4;$i++){ // use { and } to grab a string at a position (between 0 and 25), // Then we will add it to the end of our current string $st .= $str{rand(0,25)}; } // Check if the word create above is in the word list if(in_array($st,$wordList)){ // Yes it is! echo it out echo $c.'. Found: '.$st; // break ends any loop, since we found a word lets leave break; }else{ // Word was not found, lets display it an continue echo $c.'. Couldn\'t find: '.$st.'<br>'; } // Increase $c $c++; } ?> Wow you are fascinating at PHP i have absolutely no idea how your script is working. Thank you! If you would like to check out some more of my codes check out http://beta.phpsnips.com, maybe you will find some useful stuff! Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815216 Share on other sites More sharing options...
Philip Posted April 21, 2009 Share Posted April 21, 2009 Just my version of TLG's code: <?php // Array of words we will check for $wordList = array('cats','dogs','frog','mice','fart','mart','kart'); // An array of the letters $letters = range('a', 'z'); // Set the counter at one $c = 1; // Figure out the start time $starttime = microtime(); $startarray = explode(" ", $starttime); $starttime = $startarray[1] + $startarray[0]; // Start the main loop while(!in_array($st, $wordList)) { // Shuffle the letter list shuffle($letters); // Grab the first four letters: // (Note you could also use $st = implode(array_slice($letters, 0, 4)); // But I think that would use waster more system resources by running more // functions than really needed....) $st = $letters[0].$letters[1].$letters[2].$letters[3]; // Just to show what word we are on echo $c,' ',$st,'<br>'; // Increment the counter $c++; } // Figure out the finish time $endtime = microtime(); $endarray = explode(" ", $endtime); $endtime = $endarray[1] + $endarray[0]; echo 'Page loaded in ', round($endtime-$starttime, 4), ' seconds with a total of ',$c,' tries'; ?> Simplifies the while loop and shows the load time. Just FYI - it is possible for bigger words that your script could time out trying to find the correct word. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815519 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 i know this is not really a php question. But do either of you know how much like bandwidth it takes up for the serve to sit there and constantly load random letters until it reaches a word? Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815520 Share on other sites More sharing options...
Yesideez Posted April 21, 2009 Share Posted April 21, 2009 I would imagine hardly anything at all as it;s just a simple script and not doing any serious number crunching. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815528 Share on other sites More sharing options...
yakoup46 Posted April 21, 2009 Author Share Posted April 21, 2009 i kinda thought so, i mean it is just words. lol. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815531 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 Run the script (in Firefox) when it is done loading, right click and choose "View Page Info" the in the "General" tab, check its "size: xxx KB" Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815711 Share on other sites More sharing options...
gizmola Posted April 21, 2009 Share Posted April 21, 2009 i know this is not really a php question. But do either of you know how much like bandwidth it takes up for the serve to sit there and constantly load random letters until it reaches a word? It requires little to no bandwidth, because PHP is a serverside scripting language, meaning that php scripts run on the server. What this particular script does require is processing time, and the results are unpredictable because you are generating random combinations while looking for a specific combination. PHP does have a processing time limit that is configurable on the server, so it's always possible that the script could exceed that time on a particular run, and timeout, returning no results. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815724 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 Also, the bigger your word list is the less bandwidth you will probably use. I tried with one word, and it got to +500,000 results before I stopped it. With more words, it got to around +50,000 - 80,000 results till it found a word. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815732 Share on other sites More sharing options...
gizmola Posted April 21, 2009 Share Posted April 21, 2009 Also, the bigger your word list is the less bandwidth you will probably use. I tried with one word, and it got to +500,000 results before I stopped it. With more words, it got to around +50,000 - 80,000 results till it found a word. Sorry for not looking at the code. Above and beyond what I wrote previously, I assumed the code simply generated an attempt, but didn't actually output it. Since it's being output, you have the size of the html page that is ultimately being returned, which is again, completely variable. If however, the code simply checked the random combination for a match and discarded it if it did not match, then it would only be on the server. Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815736 Share on other sites More sharing options...
The Little Guy Posted April 21, 2009 Share Posted April 21, 2009 Sorry for not looking at the code. Above and beyond what I wrote previously, I assumed the code simply generated an attempt, but didn't actually output it. Since it's being output, you have the size of the html page that is ultimately being returned, which is again, completely variable. If however, the code simply checked the random combination for a match and discarded it if it did not match, then it would only be on the server. Yeah... My code outputs it to the screen, so if you didn't want that, removing the: else{ echo $c.'. Couldn\'t find: '.$st.'<br>'; } would save an extra 2-6 KB in bandwidth! Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-815746 Share on other sites More sharing options...
yakoup46 Posted April 23, 2009 Author Share Posted April 23, 2009 Okay I know i am badgering this to death. But with the script the TLG used is there anyway to make sure that if the same sequence of letters is repeated to negate it from the count? Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-817709 Share on other sites More sharing options...
The Little Guy Posted April 23, 2009 Share Posted April 23, 2009 <?php $wordList = array('cats','dogs','frog','mice','fart','mart','kart'); $str = 'abcdefghijklmnopqrstuvwxyz'; $randWords = array(); $good = FALSE; $c = 1; while(!$good){ $st = ''; for($i=0;$i<4;$i++){ $st .= $str{rand(0,25)}; } if(in_array($st,$wordList)){ echo $c.'. Found: '.$st; break; }else{ if(!in_array($st,$randWords)){ $randWords[] = $st; echo $c.'. Couldn\'t find: '.$st.'<br>'; $c++; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154978-generating-random-word/#findComment-817719 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.