![]() ![]() There is an inside out version of the Fisher-Yates shuffle algorithm, called the Durstenfeld version, that randomly distributes sequentially acquired items into arrays and collections while loading the array or collection. This answer suggests some strategies for getting what you want and ensuring they are in a random order using some already well-known algorithms. Note that this would slow down your RNG if you use a large shift-register and the maximum number you want is not a power of 2 - 1. You should embed the previous code in your RNG so that the caller wouldn't care about implementation. Here's a pseudo code as to how to get number in the range : x = LFSR.getRandomNumber() You would use a 4-bit LFSR which has a range inclusive. Say you want to have numbers between 1 and 10 (as in your example). The maximum value (and thus range) problem can solved by reusing the LFSR until you get a number within your range. From the example you gave, it seems that you are not expecting zero to be among the answers so, the second issue does not seem relevant to your case. The first drawback might not be an issue depending on your application. The third issue which is relevant to your question is that the maximum number generated by the LFSR is always a power of 2 - 1 (or power of 2 - 2). The second problem is that either the all zero word or the all one (in terms of bits) word is invalid depending on the LFSR implementation. There are some drawbacks however: by observing a small number of outputs from the RNG, one can reconstruct the LFSR and predict all values it will generate, making them not usable for cryptography and anywhere were a good RNG is needed. They are very efficient and simple to implement even in software and are guaranteed not to repeat in (2^N - 1) uses for an LFSR with an N-bit shift-register. I second gbarry's answer about using an LFSR. But for large random numbers with a good generator, just trust probability not to give you any repeats. Of course, if you have a bad random number generator (like the Debian SSL random number generator vulnerability), or are generating small enough numbers that the birthday paradox gives you a high chance of collision, then you will need to actually do something to ensure you don't get repeats. If you're using a good random number generator, that actually has enough entropy, then the odds of generating repeats are so miniscule as to not be worth worrying about.įor instance, when generating cryptographic keys, no one actually bothers checking to see if they've generated the same key before since you're trusting your random number generator that a dedicated attacker won't be able to get the same key out, then why would you expect that you would come up with the same key accidentally? ![]() ![]() If you want to creating large (say, 64 bits or greater) random numbers with no repeats, then just create them. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |