About the Character Name Generator
Posted November 11th, 2006 by Nick
Someone had the "rules to create a D&D Name" in their sig on a forum, and I thought it would be a nice exercise to create in PHP, so I created a block running the code. It should generate a new name each time the page is refreshed if you're logged in to the site. Those not logged in will get a cached result.
Here's the code to actually create the name. The rules are on the page. If I seriously screwed something up, please point it out to me... :)
<?php
function AddConsonant()
{
$consonants = array("b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z");
return ($consonants[rand(1, 20) - 1]);
}
function AddVowel()
{
$vowels = array("a", "e", "i", "o", "u", "y");
return ($vowels[rand(1, 6) - 1]);
}
function AddArticleOrPreposition()
{
$words = array(" of ", " from ", " under ", " a ", " the ", " above ");
return ($words[rand(1, 6) - 1]);
}
function CreateName()
{
$name = "";
$outerRoll = 0;
while ($outerRoll < 10 or $name == "")
{
$outerRoll = rand(1, 10);
if ($outerRoll <= 4)
$name = $name . AddConsonant();
elseif ($outerRoll <= 7)
$name = $name . AddVowel();
elseif ($outerRoll <= 9)
$name = $name . AddArticleOrPreposition();
}
return ( ucwords($name) );
}
// Main section
print CreateName();
?> 