RE: [Python, Scala, Java] Wordgenerator 06-06-2013, 05:23 PM
#6
(06-06-2013, 04:16 PM)Deque Wrote: @noize: I misunderstood your code then.
Quote:You're right here again, but, in case you thought so, my code does not give as output wordlist repeated words as it checks whether the new word as been already generated or not.
So if the caller wants too many words, your program will become extremely slow or even run infinitely.
Imagine you have lots of words in your wordlist and only one or two left. You will have to create a shitload of words until you randomly get the ones that aren't already in the list.
Edit: Drop that above, I just read you leave these words out and you can not really get all possible words. Doesn't PHP have a set? It is better for performance using a set if you want to avoid duplicates. There would also be no need to check for duplicates.
PHP does not have in-built sets as far as I know. array_unique removes duplicate values from an array, but then I could just keep on checking for duplicates.
However, the thing that it might run endlessly is true. If I give an alphabet of free litters, set wordlength to a max of 3 and ask for 100 words, it will in fact run endlessly. I just added a timeout, so that, in case it overflows the allowed number of consecutive errors it tries using the same litters for the same word and then, if it still fails, quits.
Latest code update (where t is timeout in secs):
PHP Code:
function wordGenerator(alphabet, m, n, o, t) {
$genOut = array();
$curt = time();
for (i = 0; i => o; i++) {
$x = rand(m, n);
$randPick = array(array_slice(alphabet, 0, $x));
$randWord = "";
foreach ($randPick as $randValue) {
$randWord .= $randValue;
$curt = time();
}
if (!isset($genOut[$randWord])) {
$genOut[] = $randWord;
} else {
$i = $i - 1;
}
shuffle(alphabet);
if (time() => $curt + t) {break;}
}
}
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U
If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U
If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)