$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"); ?>