rendered paste body<?php
ob_start();
class Namegen {
var $mchash; // Markov chain hashtable.
var $order; // Order of the chain
var $start_char; // Start character
var $end_char; // End character
function Namegen() {
}
function get_from_file($filename) {
$handle = fopen($filename, 'r');
if (!$handle) { return false; }
$bufsize = 4096;
$oldbuf = "";
$this->$mchash = array();
$gotvals = false;
while ($buffer = fread($handle, $bufsize)) {
$buffer = $oldbuf . $buffer;
$curarray = explode("\n", $buffer);
$n = count($curarray);
$oldbuf = $curarray[$n - 1];
if (!$gotvals) {
list($this->$order, $this->$start_char, $this->$end_char) = explode(" ", $curarray[0]);
$i = 1;
$gotvals = true;
} else {
$i = 0;
}
for(; $i < $n - 1; $i++) {
$line = $curarray[$i];
$input = explode(" ", $line);
$key = $input[0];
$vals = array();
$m = count($input);
for($j = 1; $j < $m; $j += 2) {
$vals[$input[$j]] = $input[$j + 1];
}
$this->$mchash[$key] = $vals;
}
}
fclose($handle);
return true;
}
function weighted_rand($array) {
$keys = array_keys($array);
$cumul = array();
$n = count($keys);
$curr = 0;
for($i = 0; $i < $n; $i++) {
$curr += $array[$keys[$i]];
$cumul[$i] = $curr;
}
$sel = mt_rand(0, $curr);
for($i = 0; $i < $n; $i++) {
if ($cumul[$i] >= $sel) { return $keys[$i]; }
}
return $keys[$n - 1];
}
function gen_name() {
$state = str_repeat($this->$start_char, $this->$order);
$curr_name = "";
while (1) {
$curr_arr = $this->$mchash[$state];
if (!$curr_arr) { return $curr_name; }
$next = weighted_rand($curr_arr);
if ($next == $this->$end_char) { return $curr_name; }
$curr_name = $curr_name . $next;
$state = $state . $next;
$state = substr($state, strlen($state) - $this->$order, $this->$order);
}
}
}
$last = new Namegen;
$last->get_from_file("dist.all.last.hash");
?>
<html><head><title>Random Name Generator</title></head>
<body>
<?php echo $last->gen_name(); ?>
</body></html>