You could use Format-Preserving Encryption to encrypt a counter. So I will simply go with 1, and hope it's not an issue, however if it is I will go with the deterministic solution of generating all the numbers and storing them so that there is a guarentee of getting a new random number, and I can use "small" numbers (i.e. less than a billion (or a thousand million for your europeans =)). generating a random number that is already taken) but will also allow me to keep the number "small" (i.e. unix timestamp) to reduce chances of a collision, again I get larger numbers than I need.Īnyone have any clever ideas that will reduce the chances of a "collision" (i.e. using MD5) to get a smaller numeric value, we should rarely get collisions, but I end up with larger than needed numbers again.Ħ) Prepend or append time based information to the random number (i.e. If it has been used add or subtract another randomly generated random number and check again, problem is we're back to simply generating random numbers and checking as in solution 1.Ĥ) Suck it up and generate the random list and save it, have a daemon put them into a Queue so there are numbers available (and avoid constantly opening and closing a file, batching it instead).ĥ) Generate much larger random numbers and hash them (i.e. Problem is this is no longer a random number as I have introduced bias (eventually I will get clumps of numbers and you'd be able to predict the next number with a better chance of success).ģ) Generate a random number and then check if it has already been used. If it has been used add or subtract one from the number and check again, keep repeating until I hit an unused number. Possible fix: use a very large pool of numbers to reduce the chances of this (but then I end up with silly long numbers).Ģ) Generate a random number and then check if it has already been used. Problem here is that I may get unlucky and generate a lot of used numbers before getting one that is unused. If it has been used generate a new number, check, repeat as needed until I find an unused one. Problem is that the solutions I can think of have problems:ġ) Generate a random number and then check if it has already been used. Problem is I'd like to avoid having to store a list of numbers (and thus read the file, pop one off the top, save the file and close it). No big deal right? Just generate a list of numbers, shove them into an array and use Python "random.shuffle(big_number_array)" and we're done. The program generating them doesn't run forever so it can't rely on internal state. These numbers are stored in a database table (indexed) along with the information linked to them. guessing the next one that will come up). Here is what I want: I need Python to generate a simple a list of numbers from 0 to 1,000,000,000 in random order to be used for serial numbers (using a random number so that you can't tell how many have been assigned or do timing attacks as easily, i.e. Ok this is one of those trickier than it sounds questions so I'm turning to stack overflow because I can't think of a good answer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |