tags:

views:

429

answers:

9

I want to generate random strings like:

sssder
tvmwww
66rfdd
123123
oo007oo
1234
2020
1111
rrrr
r8r8r
uiuiu
wewewe
fefefe
abced
xyz..

Specifically, I want a string with a length of 5 to 8 characters that is easy to remember. Is this possible?

+1  A: 

It depends on what you want from the strings. From the examples you provided, it seems as if you want a two-step thing; e.g. a function that generates a three-character random string, then doubles each character; or repeats the string; or repeats the first character three times, the second two times, and the third but once.

Basically what you probably want to do is to make a "pool", say a suitable short string that's generated with a randomly selected method (all numbers, sequential numbers with random start, sequential letters, word parts selected at random from a dictionary...) and then a function that inflates the string according to some principle.

Williham Totland
+1  A: 

Try this: Random Alphanumeric String Generator Script in PHP

http://www.i-fubar.com/random-string-generator.php

Disco
That is a rather ugly and inefficient piece of code, I bet I could archive the exact same result in 5 lines or less.
Alix Axel
+1 eyze, that is indeed some evil-looking code.
Steve Claridge
+1  A: 

If this is for random id's or something similiar I recommend

uniqid(...)

http://us3.php.net/manual/en/function.uniqid.php>http://us3.php.net/manual/en/function.uniqid.php

Ben Reisner
+4  A: 

Here is source for a rather overcomplicated script that walks a BNF-like definition of a string and generates a matching string by randomly selecting possibilities. These pages contain some example definitions. Perhaps that may be of use.

moonshadow
A: 

stop downvoting me, he changed his question!

in c++:

string str = "";
while (rand()&1)
    str += (char)(('z'-'A')*rand()/RAND_MAX + 'A');
Inverse
+1  A: 

Use a genetic algorithm. Set up your fitness function to decide how "random" your string is (i.e. are two consonants adjacent? Well that's not as good as a symbol or number next to a consonant... but how far apart are the consonants in the alphabet? are they the same case?) Let it run for a couple days, and you'll be guaranteed to find the fanciest, most random 5-8 character string you'd ever hoped for.

snicker
I neglected to address your concern of "easy to remember". Your fitness function could also monitor proximity of letters on the keyboard, phonetic similarity to words in the users language, funny patterns on the numpad, etc.
snicker
+2  A: 

See this post for a short PHP function to generate a random usable keyboard characters string of a specified length (as this looks like a password generator). Here is the function copied from that post.

function rand_char($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(mt_rand(33, 126));
  }
  return $random;
}
St. John Johnson
This is what I was gonna post.
Leo Jweda
+1  A: 

I'll start with a few personal conjectures about "easy to remember":

A string is usually easy to remember if there are one or more patterns, such as:

  • it has repetitions of characters
  • it has sequences of characters (a-b-c, 1-2-3)
  • it "sounds" like a word (sequences of phonemes that mimic real words: "diskow")

Write up a program that "scores" random sequences generated based on the rules, and take the top scorers. It's like a Monte Carlo method for finding the output you want. You can adjust your scoring method if you don't like the output.

Of course, there are other "easy to remember" strings that don't fit the above:

  • if it has personal significance (your birthday)
  • if it is repeated A LOT... (everyone knows pi = 3.1415 because we're trained to know it) Although, I'd argue that PI is better described as easy to recall, rather than easy to remember (store in memory).
Jeff Meatball Yang
+2  A: 

What you're looking for is a mnemonic string generator, here is the function:

function Mnemonic($letters = 6)
{
    $result = null;
    $charset = array
    (
     0 => array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'),
     1 => array('a', 'e', 'i', 'o', 'u'),
    );

    for ($i = 0; $i < $letters; $i++)
    {
     $result .= $charset[$i % 2][array_rand($charset[$i % 2])];
    }

    return $result;
}

Updated to allow digits at the end of the string:

function Mnemonic($letters = 6, $digits = 2)
{
    $result = null;
    $charset = array
    (
     0 => array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'),
     1 => array('a', 'e', 'i', 'o', 'u'),
    );

    for ($i = 0; $i < $letters; $i++)
    {
     $result .= $charset[$i % 2][array_rand($charset[$i % 2])];
    }

    for ($i = 0; $i < $digits; $i++)
    {
     $result .= mt_rand(0, 9);
    }

    return $result;
}
Alix Axel
yes This is what i exactly needed thank you eyze :)
coderex