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?
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.
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;
}
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;
}
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.
Try this: Random Alphanumeric String Generator Script in PHP
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
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.
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:
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;
}