Jump to content

Can you help me optimize my 'random name' script?


MrTech

Recommended Posts

I am not a programmer but am teaching myself a little bit at a time. The script is not pretty but is functional...I am sure there is a better way to do it than repeating the code 5 times but that's way over my head at this point. Can some friendly person help me with the following tasks?

 

  • optimize for speed and proper php coding technique
  • generate a list of 5 random names taken from file1 and file2
  • important that words don't appear twice in the same list
  • when the 'refresh' button is clicked, does the entire page have to be reloaded? would rather just have the list of 5 items reload instead...is this possible? The reason is I'm worried that it will use up too much bandwidth if people keep reloading the page a thousand times.

 

<?
function RandomAdjective($filename) { 
$adjective = file($filename) ; 
return $adjective[array_rand($adjective)] ; 
} 
function RandomAdjective2($filename) { 
$adjective2 = file($filename) ; 
return $adjective2[array_rand($adjective2)] ; 
} 
function RandomAdjective3($filename) { 
$adjective3 = file($filename) ; 
return $adjective3[array_rand($adjective3)] ; 
} 
function RandomAdjective4($filename) { 
$adjective4 = file($filename) ; 
return $adjective4[array_rand($adjective4)] ; 
} 
function RandomAdjective5($filename) { 
$adjective5 = file($filename) ; 
return $adjective5[array_rand($adjective5)] ; 
} 
function RandomNoun($filename) { 
$noun = file($filename) ; 
return $noun[array_rand($noun)] ; 
}
function RandomNoun2($filename) { 
$noun2 = file($filename) ; 
return $noun2[array_rand($noun2)] ; 
}
function RandomNoun3($filename) { 
$noun3 = file($filename) ; 
return $noun3[array_rand($noun3)] ; 
}
function RandomNoun4($filename) { 
$noun4 = file($filename) ; 
return $noun4[array_rand($noun4)] ; 
}
function RandomNoun5($filename) { 
$noun5 = file($filename) ; 
return $noun5[array_rand($noun5)] ; 
}
$yadjective = RandomAdjective("adjectives.txt"); 
$ynoun = RandomNoun("nouns.txt");
$yadjective2 = RandomAdjective2("adjectives.txt"); 
$ynoun2 = RandomNoun2("nouns.txt");
$yadjective3 = RandomAdjective3("djectives.txt"); 
$ynoun3 = RandomNoun3("nouns.txt");
$yadjective4 = RandomAdjective4("adjectives.txt"); 
$ynoun4 = RandomNoun4("nouns.txt");
$yadjective5 = RandomAdjective5("adjectives.txt"); 
$ynoun5 = RandomNoun5("nouns.txt");
echo trim($yadjective) . ($ynoun) ;
echo "
";
echo trim($yadjective2) . ($ynoun2) ;
echo "
";
echo trim($yadjective3) . ($ynoun3) ;
echo "
";
echo trim($yadjective4) . ($ynoun4) ;
echo "
";
echo trim($yadjective5) . ($ynoun5) ;
?>

<form><INPUT TYPE="button" VALUE="Reload" onClick='parent.location="javascript:location.reload()"'></form>

 

Thanks for any help you can offer...

Link to comment
Share on other sites

Most of your fixes are actually quite easy. The thing is that even though you made functions, you were copying them when you could have combined them all since they all did exactly the same thing. That alone removes a LOT of bulk from your code.

 

<?php
// Get $numWords random words from $filename
function randomWords($filename, $numWords) {
$words = file($filename);
shuffle($words); // Mix up the words
return array_slice($words, 0, $numWords); // Get $numWords unique words
}

$numWords = 5;
$randAdjectives = randomWords('adjectives.txt', $numWords);
$randNouns = randomWords('nouns.txt', $numWords);

for($i = 0; $i < $numWords; ++$i) {
echo trim($randAdjectives[$i]) . trim($randNouns[$i]) . "\n";
}
?>

<form><INPUT TYPE="button" VALUE="Reload" onClick='parent.location="javascript:location.reload()"'></form>

 

This also makes sure there are no repeat adjectives and nouns.

 

I'm not sure what you mean by "generate a list of 5 random names taken from file1 and file2" isn't that what this does already?

 

As for not refreshing the whole page, just refreshing the words, the javascript alone required to do that would use up more bandwidth than your script currently does.

 

edit: a little clarity and a bug

edit 2: another bug, I really should test code that I write after 10 PM before posting it

Link to comment
Share on other sites

Do you have to use only those two files (adjectives.txt and nouns.txt)? Or you have more?

No I don't...it could be called firstname.txt and lastname.txt as I am trying to create a random name from two different lists. But if I should use more files please point the way, but as it stands right now I am only reading from these two files since it seems to give me the results I need.
Link to comment
Share on other sites

Most of your fixes are actually quite easy.

Fantastic! That does the trick and thanks for the tip about javascript and bandwidth usage. One last question, all the results appear on the same line, I noticed you are using the "\n"; and I had tried using that myself without any luck. What's the easiest way to force a line break between results?

 

Thanks again to you and to aeonsky for taking time out to lend a helping hand to some online stranger...

Link to comment
Share on other sites

Most of your fixes are actually quite easy.

Fantastic! That does the trick and thanks for the tip about javascript and bandwidth usage. One last question, all the results appear on the same line, I noticed you are using the "\n"; and I had tried using that myself without any luck. What's the easiest way to force a line break between results?

 

Thanks again to you and to aeonsky for taking time out to lend a helping hand to some online stranger...

 

I'm not sure if this is the easiest way, but I use this to make a line break:

 

print "<br>";

Link to comment
Share on other sites

Fantastic! That does the trick and thanks for the tip about javascript and bandwidth usage. One last question, all the results appear on the same line, I noticed you are using the "\n"; and I had tried using that myself without any luck. What's the easiest way to force a line break between results?

 

Thanks again to you and to aeonsky for taking time out to lend a helping hand to some online stranger...

 

To clarify about the javascript, it doesn't actually use ANY bandwidth to RUN javascript, but the controls the user would need to originally download would more than likely outweigh the bandwidth required to download the reload button (which is all that's being downloaded that is extra). \n works as a line break only when it's between double quotes ("). If you truly want it to appear as a line break in a web browser, you'll need to use

<br />

like the poster above me said.

 

edit: Typos! I actually called javascript java! The cardinal sin!

Link to comment
Share on other sites

Thanks again for the quick answer on something so easy and trivial as a line break.

 

It's all working perfectly now and I'm off to go figure out formatting so I can display the results in two columns and make it all pretty and what not.

Link to comment
Share on other sites

Thanks again for the quick answer on something so easy and trivial as a line break.

 

It's all working perfectly now and I'm off to go figure out formatting so I can display the results in two columns and make it all pretty and what not.

 

You'll be using a table, with each call to the function in it's own <td> block.

Link to comment
Share on other sites

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.