Quote:
Originally Posted by Tigairius
Right now your while loop will go in to an infinite loop if your input limit is greater than the array size of the source.
|
Oh, I see. Perhaps something like this?
PHP Code:
function randomstring2(source, limit, repeat) {
if(temp.limit == null) {
temp.limit = temp.source.size();
}
if(temp.limit > temp.source.size()) {
temp.limit = temp.source.size();
}
while(temp.i < temp.limit) {
temp.trial = temp.source[random(0, temp.source.size())];
if(temp.repeat) {
temp.output @= temp.trial;
temp.i++;
}
else {
if(temp.output.pos(temp.trial) < 0) {
temp.output @= temp.trial;
temp.i++;
}
}
}
return temp.output;
}
Quote:
Originally Posted by DustyPorViva
I agree that arrays are terrible for this sort of thing, simply because of the time it takes to set them up. I much prefer strings.
|
Well, I provided a source array that contains all possible single characters. So, it shouldn't take too much time to edit it, if you're simply looking to generate something from a list of single characters. Arrays offer the possibility of doing something like this:
PHP Code:
function onCreated() {
temp.source = {
"test",
"sources",
"foo bar",
"foo bar baz",
"foo!bar!baz"
};
echo(randomstring2(temp.source, 3, true));
echo(randomstring2(temp.source, 3, false));
}
Which outputs:
Quote:
foo!bar!bazfoo bar bazsources
sourcestestfoo bar baz
|