in reply to Check randomly generated numbers have not been used before

Hello,

Are you supposed to read the previously generated numbers list from the file each time ? Or are you allowed to cache these results ?

I mean, if you have to read 300.000 numbers from a text file, each and every time you run your script, then generate a number, check that it's not already generated, loop until you have an "original" number, then write down all the previously generated numbers AND the new, original one, yes, it would be costly.

However, you may use a hash to store the generated numbers. One hash is enough, with the numbers being the keys (you don't even need anything as the values, 1 or undef will work just as well). This should make the search for previous occurences happen in constant time (and I mean FAST).

You might index the previously generated numbers in a "clever" way, maybe bitwise, in your TXT, so that you do NOT have to read a whole, huge, file each time (you may store 8 bits per byte, each of them could represent one of the combinations).

You could sort your file, but that would only be relevant if you had to search through it for each number - which you really shouldn't do, whether you generate only ONE new number per run, or a whole batch of them, as you might have to reroll each of them based on previously generated numbers, you might as well put them all in memory.

As for writing your file, you really should append the new result to the end of it, without rewriting previous entries, unless you wish to keep it sorted.

  • Comment on Re: Check randomly generated numbers have not been used before