Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/154978-generating-random-word/
Share on other sites

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.

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.';
?>

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++;
}
?>

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.

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!

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.

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. 

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.

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!

<?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++;
      }
   }
   
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.