Whether you're using a database (which I would strongly recommend), or a text-file, or something else, there is the problem of checking new keys against used keys; this will not be a problem at first, since you will have few used keys to begin with. If you add a lot of numbers though, validating a new key will take more time, as there will be more keys to compare to.
You could solve this by generating all the keys once, to begin with, and delete used keys (or mark them as used) each time a key is used.
Without getting to specific about the details: You could for instance generate 500'000 rows (or more) in a table, and include a row-number, a PK, and a field marking a row as "used". When you need a new key, select the PK from a random row, and mark that row as used (or delete it, if you prefer).
If you use a text-file, you could do the same, making sure to delete each "picked" line as you go.