random snippets

How to generate a pronouncable password with PHP

Tagged password, generate, random, php  Languages php

This is a slight modification of a script I found at PHPFAQ:

class Password
    static function generate($length)

        $vowels = array("a", "e", "i", "o", "u");
        $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
        "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); 

        $num_vowels = count($vowels);
        $num_cons = count($cons);

        $password = '';
        for($i = 0; $i < $length; $i++)
            $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];

        return substr($password, 0, $length);

To use it try this code:

require 'password.php'

echo Password.generate(10);

It will spit out passwords that are semi-pronouncable.

A random password generator for Ruby

Tagged random, password, ruby  Languages ruby

This is a simple random password generator for Ruby that allows you to customize the characters included in the password:

CHARS = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
def random_password(length=10)
  CHARS.sort_by { rand }.join[0...length]

In this example, the generated passwords contain numbers, uppercase, and lowercase characters.

How to create random data with Postgres

Tagged postgres, random, data  Languages sql

This example creates random data from a predefined list of valid values:

insert into transactions(description, price, timestamp) values(
  ('{Bought cat,Bought dog,Bought house}'::text[])[ceil(random()*3)],
  NOW() - '1 day'::INTERVAL * ROUND(RANDOM() * 100)

This sets a column to a random UUID:

UPDATE customers SET name = uuid_in(md5(random()::text || clock_timestamp()::text)::cstring);