Generating non-repeating random numbers

后端 未结 5 642
旧时难觅i
旧时难觅i 2020-11-28 00:08

I want to create a function in C. It will return a random integer in-range of N like:- rand() % N; but the thing is I want to keep track of uniqueness. I don\'t want the num

相关标签:
5条回答
  • 2020-11-28 00:16

    Use some sort of hash table to store the already generated number and to fast check the number is already seen. I don't know exacltly what you are trying to do, but since you are requiring unique rand, I guess you are trying to permutate a finite set, if it is the case, have a look at some Shuffling Algorithms.

    0 讨论(0)
  • 2020-11-28 00:24

    It sounds like what you want is really a random permutation of the number 1..N. So, fill an array with consecutive integers 1..N and then shuffle the array. There are well known algorithms for shuffling that you can look up.

    0 讨论(0)
  • 2020-11-28 00:27

    Four options, all of which are O(1) in both memory and time:

    1. Just increment a global counter. Since you want uniqueness, you can't generate random numbers anyway.
    2. Generate a number from a set sufficiently large that it is highly improbable that a number repeats. 64 bits is sufficient for in-app uniqueness; 128 bits is sufficient for global uniqueness. This is how UUIDs work.
    3. If option 1 isn't "random" enough for you, use the CRC-32 hash of said global (32-bit) counter. There is a 1-to-1 mapping (bijection) between N-bit integers and their CRC-N so uniqueness will still be guaranteed.
    4. Generate numbers using a Linear Feedback Shift Register. These are N-bit counters which count in a seemingly "random" (though obviously deterministic) pattern. For your purposes, this would essentially be a modestly faster version of option 3.
    0 讨论(0)
  • 2020-11-28 00:34

    Rather than an array, you could use a super fast an efficient bloom filter. If you are generating any large quantity of numbers, this will be WAY faster than looping through an array.

    0 讨论(0)
  • 2020-11-28 00:35

    The algorithm described is pretty bad because it searches through the new array for each new entry. This means it has to search through more and more data as the array grows, and worse, as the number of remaining item decreases, it will end up looping more.

    For example, if you have a list from 1…10, when you have filled eight items, there are only two items left (say, 7 and 9), now, each time you generate a random number, it will generate a non-unique number 80% of the time, and have to scan through at least six entries before detecting the duplicate.

    There’s probably some even better methods in some libraries, but a better way than the one in the question would be to create a (linked) list of items, pick one at random, remove it, and add it to the new list. That way, every time you pick one a random one, it is guaranteed to be unique because the used ones are no longer in the pool.

    0 讨论(0)
提交回复
热议问题