Skip to content

Instantly share code, notes, and snippets.

@katanacrimson
Created July 8, 2010 18:28
Show Gist options
  • Save katanacrimson/468389 to your computer and use it in GitHub Desktop.
Save katanacrimson/468389 to your computer and use it in GitHub Desktop.
<?php
benchmark();
$max_iterations = 36;
$pattern_hashes = array_merge(range(0,9), range('a', 'z'));
$KP = new KeyPatterns();
$i = 0;
do
{
$patterns[] = $KP->getRandomKeyPattern($pattern_hashes[$i]);
$i++;
}
while($i < $max_iterations);
foreach($patterns as $pattern)
echo $pattern . PHP_EOL;
//var_dump($patterns);
//echo PHP_EOL;
benchmark();
die();
//echo $KP->getRandomKeyPattern('a') . PHP_EOL;
/**
* Shrike - Key pattern class,
* Handles the construction of key patterns.
*
*
* @category Shrike
* @package Shrike
* @author Damian Bushong
* @license MIT License
* @link http://github.com/Obsidian1510/Shrike
*/
class KeyPatterns
{
/**
* @const - The minimum number of static characters that must be present in each key pattern
*/
const MIN_STATIC_CHARS = 10;
const TOTAL_LIMIT_CHUNKS = 7;
const TOTAL_TIME_CHUNKS = 7;
protected $static_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_';
protected function getRandomStaticChar()
{
return $this->static_chars[mt_rand(0, strlen($this->static_chars) - 1)];
}
protected function getChunkReplacement($is_limit_chunk, $chunk_number)
{
if($is_limit_chunk)
{
// Limit key chunk
return '%' . $chunk_number . '$s';
}
else
{
// Time chunk
return '%' . ($chunk_number + self::TOTAL_LIMIT_CHUNKS) . '$s';
}
}
public function getRandomKeyPattern($identifier)
{
// Initialize the mt_rand function for proper use
mt_srand((double) microtime() * (9001 + (date('j') * 1000))); // <insert obligatory over-9000 joke here>
$pattern = '';
$static_chars_used = 0;
$limit_chunks_available = range(1, self::TOTAL_LIMIT_CHUNKS);
$time_chunks_available = range(1, self::TOTAL_TIME_CHUNKS);
$limit_flip = array_flip($limit_chunks_available);
$time_flip = array_flip($time_chunks_available);
do
{
$random_input = rand(1,10);
// 20% of adding in static characters
if($random_input > 2)
{
// 40% chance of limit key chunk vs 40% of time chunk
// Automatically fall back on the limit chunks to finish things if we've taken care of all the time chunks, and vice versa
if(!sizeof($time_chunks_available) || ($random_input <= 6 && sizeof($limit_chunks_available)))
{
$chunk = mt_rand(1, sizeof($limit_chunks_available));
$chunk_val = array_slice($limit_chunks_available, $chunk - 1, 1);
$pattern .= $this->getChunkReplacement(false, $chunk_val[0]);
unset($limit_chunks_available[$limit_flip[$chunk_val[0]]]);
}
else
{
$chunk = mt_rand(1, sizeof($time_chunks_available));
$chunk_val = array_slice($time_chunks_available, $chunk - 1, 1);
$pattern .= $this->getChunkReplacement(false, $chunk_val[0]);
unset($time_chunks_available[$time_flip[$chunk_val[0]]]);
}
}
else
{
$chars = mt_rand(2, 7);
$chars = self::MIN_STATIC_CHARS - $static_chars_used;
while($chars !== 0)
{
$pattern .= $this->getRandomStaticChar();
$chars--;
}
$static_chars_used += $chars;
}
}
while((sizeof($limit_chunks_available) + sizeof($time_chunks_available)) > 0);
if($static_chars_used < self::MIN_STATIC_CHARS)
{
$chars = self::MIN_STATIC_CHARS - $static_chars_used;
while($chars !== 0)
{
$pattern .= $this->getRandomStaticChar();
$chars--;
}
$static_chars_used += $chars;
}
return "#$identifier#$pattern";
}
}
function getTime()
{
$timer = explode(' ', microtime());
return $timer[1] + $timer[0];
}
function benchmark()
{
static $start = 0;
if(!$start)
$start = getTime();
else
echo round(getTime() - $start, 6) . ' seconds' . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment