I needed to generate a random password. There’s plenty of scripts out there for doing this, but most involve creating a dictionary of characters, then looping for a given number of times generating a random number and picking out a character based on the number. I wanted a more compact solution so here’s what I came up with:

$chars = str_split(sha1(rand()),2);
foreach ($chars as $k => $char) {
    $chars[$k] = chr((hexdec($char)/255*95)+31);
}
$password = implode($chars);

It’s PHP, but can probably be replicated easily in other languages. Here’s what it does:

  1. Generates an SHA1 hash of a random number, then splits the hash into groups of 2 characters (each is a hexadecimal number =<255).
  2. Converts each hex number into decimal, scales it to value between 32 and 127, then converts that number to an ASCII character
  3. Creates a string from the array of characters