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?
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?
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.
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
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.
stop downvoting me, he changed his question!
in c++:
string str = "";
while (rand()&1)
str += (char)(('z'-'A')*rand()/RAND_MAX + '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.
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;
}
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:
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:
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;
}