Random sentence generator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
npodges
Forum Newbie
Posts: 12
Joined: Tue Aug 12, 2003 5:55 pm

Random sentence generator

Post by npodges »

hey, this is a simple code i wrote to hopefully randomly generate a sentence. i have 7 text files with different work lists. one of these is the list of sentence structures. for instance: article noun verb article object
some sentences seem to be coming up quite often, and i realize that i used kinda small lists (about 10 words per) but there shouldnt be definate patterns like that. anyways, the site is
http://goomba300.port5.com/sentence.php
heres the code:

Code: Select all

<html>
<body>
<?

$sentences_array = file('sentences.txt');
$sentence = $sentences_array&#1111;rand(0, count($sentences_array) - 1)];
$sentence = trim($sentence);

$adjs_array = file('adjs.txt');
$adj1 = $adjs_array&#1111;rand(0, count($adjs_array) - 1)];
$adj1 = trim($adj1);
$adj2 = $adjs_array&#1111;rand(0, count($adjs_array) - 1)];
$adj2 = trim($adj2);

$advs_array = file('advs.txt');
$adv1 = $advs_array&#1111;rand(0, count($advs_array) - 1)];
$adv1 = trim($adv1);
$adv2 = $advs_array&#1111;rand(0, count($advs_array) - 1)];
$adv2 = trim($adv2);

$verbs_array = file('verbs.txt');
$verb1 = $verbs_array&#1111;rand(0, count($verbs_array) - 1)];
$verb1 = trim($verb1);
$verb2 = $verbs_array&#1111;rand(0, count($verbs_array) - 1)];
$verb2 = trim($verb2);

$snouns_array = file('snouns.txt');
$snoun1 = $snouns_array&#1111;rand(0, count($snouns_array) - 1)];
$snoun1 = trim($snoun1);
$snoun2 = $snouns_array&#1111;rand(0, count($snouns_array) - 1)];
$snoun2 = trim($snoun2);

$pnouns_array = file('pnouns.txt');
$pnoun1 = $pnouns_array&#1111;rand(0, count($pnouns_array) - 1)];
$pnoun1 = trim($pnoun1);
$pnoun2 = $pnouns_array&#1111;rand(0, count($pnouns_array) - 1)];
$pnoun2 = trim($pnoun2);

$preps_array = file('preps.txt');
$prep1 = $preps_array&#1111;rand(0, count($preps_array) - 1)];
$prep1 = trim($prep1);
$prep2 = $preps_array&#1111;rand(0, count($preps_array) - 1)];
$prep2= trim($prep2);
$art = "the";

eval ("\$sentence = "$sentence";");
echo $sentence. "\n";
echo "<p></p>";
echo "<A HREF="http://goomba300.port5.com/sentence.php" TARGET="_self">refresh.</A>";
?>
</body>
</html>
any ideas why i see these patterns?
thanks,
nick podges
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

I know that rand() doesn't produce very good pseudo-random numbers, you may want to write a better function yourself.
Post Reply