How can I create an algorithm to generate random strings?

I want to generate random lines like:

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

      

In particular, I want a string that is 5 to 8 characters long that is easy to remember . Is it possible?

+1


source to share


9 replies


What you are 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 enter numbers at the end of the line:

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;
}

      

+4


source


Here is the source of a rather complex script that executes a BNF-like string definition and generates a matching string by randomly selecting possibilities. These pages contain some sample definitions. Perhaps this could be helpful.



+4


source


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

function rand_char($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(mt_rand(33, 126));
  }
  return $random;
}

      

+2


source


It depends what you want from the strings. From the examples you gave it sounds like you want a two-step thing; for example a function that generates a three-character random string, then doubles each character; or repeats a line; or repeats the first character three times, the second two times, and the third one but once.

Basically what you probably want to do is create a "pool", for example a suitable short string that is randomly generated by a random method (all numbers, random start sequence numbers, sequential letters, word parts randomly selected from dictionary ...) and then a function that inflates the string according to some principle.

+1


source


Try this: Script random alphanumeric string generator in PHP

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

+1


source


If it is for a random id or something similar, I recommend

uniqid (...)

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

+1


source


Use a genetic algorithm. Customize your fitness function to decide how "random" your string is (ie are two consonants adjacent?) Well, that's not as good as a character or number next to a consonant ... but how far apart are the consonants alphabet? are they in the same case?) Let it run for a couple of days, and you're guaranteed to have the freshest, most random 5-8 character string you ever hoped for.

+1


source


I'll start with a few personal guesses about "catchy":

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

  • has character repetitions
  • it has character sequences (abc, 1-2-3)
  • "sounds" like a word (sequences of phonemes that mimic real words: "disc")

Write a program that scores random rule-based sequences and top scorers. This is similar to the Monte Carlo method for finding the result you want. You can customize your scoring method if you don't like the conclusion.

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

  • if it has a personal meaning (your birthday)
  • if it repeats A LOT ... (everyone knows pi = 3.1415 because we are trained to recognize it) Although I would say that PI is better described as easy to remember, rather than easy to remember (store in memory).
+1


source


An easy approach could be:

1) initialize an empty string
2) generates a random number between [0.25]
3) Add 97 to this number
4) create a character for this number
5) add a character to an existing string
* follow steps 1-5 for the number of times equal to the length of the string ...

say I need lines 6-10 long. for each line 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;
}

      

0


source







All Articles