2

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?

coderex
  • 27,225
  • 45
  • 116
  • 170
  • 14
    Define "fancy"... – sth Aug 21 '09 at 17:14
  • 1
    What is your definition of fancy? Where do you want to use these strings? – Raminder Aug 21 '09 at 17:14
  • 1
    Define fancy. Do you mean random lower case characters and numbers? – cherouvim Aug 21 '09 at 17:14
  • 1
    Do you want the characters and length to be purely random? – Graphics Noob Aug 21 '09 at 17:15
  • We really *don't* understand what you mean. Typing more examples doesn't help. Please tell us exactly what properties you wish the string to have. How long should it be? What types of characters may appear (e.g. punctuation? whitespace?)? Should any of the characters be dependent on other characters, or should they all be independently random? – Tyler McHenry Aug 21 '09 at 17:20
  • Okay, thanks for the clarification on the length. The problem is that "easy to remember" is an entirely subjective concept that a computer can't really make decisions about for you unless you give it a more specific mathematics-based definition. – Tyler McHenry Aug 21 '09 at 17:29
  • Use a dictionary to generate nonsense sentences 5-8 words long, display the sentence and highlight the first letters ;) – moonshadow Aug 21 '09 at 17:33
  • @Tyler: Yes that is why i asked is this possible.. hehh, and many other f***s trying to down vote i don't know why those people have such a shameless activity. :). any way thanks for you answer :) – coderex Aug 21 '09 at 17:35
  • 2
    The reason your question has been voted down is because it's not well-defined. It's as if you had posted "how do I program a computer to generate pretty pictures?" without giving a definition of "pretty" that a computer can understand. – Tyler McHenry Aug 21 '09 at 17:47
  • Well, I believe that he's trying to get strings that are easier to spell, example "mabokufi" is easier (at least for me) to remember than "lxkrpuol". Easier to Spell = Easier to Remember. – Alix Axel Aug 21 '09 at 20:51

9 Answers9

5

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
  • 86,889
  • 7
  • 82
  • 122
5

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
  • 151,645
  • 95
  • 393
  • 500
3

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;
}
Community
  • 1
  • 1
St. John Johnson
  • 6,590
  • 7
  • 35
  • 56
2

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
  • 28,471
  • 6
  • 52
  • 68
2

Try this: Random Alphanumeric String Generator Script in PHP

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

Disco
  • 515
  • 1
  • 6
  • 14
  • 1
    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 Aug 21 '09 at 20:46
2

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
  • 662
  • 6
  • 16
2

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
  • 6,080
  • 6
  • 43
  • 49
  • 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 Aug 21 '09 at 21:12
2

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
  • 37,839
  • 27
  • 91
  • 125
1

An easy approach may be :

1) initialize an empty string
2) generate a random number between[0,25]
3) Add 97 to that number
4) generate a character for that number
5) append the character to the existing string
* run steps 1-5 for the number of times , equal to the length of the string.

say i want strings of length 6-10. for each string you need to run this piece of code

i = rand (6,10)
str = "";
while(i--)
{
   num = rand (97,97+25);
   c = chr(num);
   str = str.c;
}
Azad Salam
  • 146
  • 7