Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure that randomly generated numbers are not being repeated? [duplicate]

Tags:

c++

random

qt

Possible Duplicates:
Unique (non-repeating) random numbers in O(1)?
How do you efficiently generate a list of K non-repeating integers between 0 and an upper bound N

I want to generate random number in a certain diapason, and I must be sure, that each new number is not a duplicate of formers. One solution is to store formerly generated numbers in a container and each new number checks aginst the container. If there is such number in the container, then we generate agin, else we use and add it to the container. But with each new number this operation is becoming slower and slower. Is there any better approach, or any rand function that can work faster and ensure uniqueness of the generation?

EDIT: Yes, there is a limit (for example from 0 to 1.000.000.000). But I want to generate 100.000 unique numbers! (Would be great if the solution will be by using Qt features.)

like image 462
Narek Avatar asked Jun 22 '10 12:06

Narek


People also ask

How will you generate a random number which does not get repeated?

We do this by shuffling the numbers as we generate the random numbers, instead of doing it beforehand. This is done by keeping track of how many numbers we've generated so far, and for each new number, we pick a random one from the unused set, swap it into the used set and then return it.

How do you use Randbetween without repeating numbers?

As both RAND and RANDBETWEEN recalculate with every change on the worksheet, your list of random numbers will be continuously changing. To prevent this from happening, use Paste Special > Values to convert formulas to values as explained in How to stop random numbers from recalculating. Delete duplicates.

How do you generate a list of random numbers without duplicates or repeats in Python?

Using randint() & append() functions Use the randint() function(Returns a random number within the specified range) of the random module, to generate a random number in the range in specified range i.e, from 1 to 100.


2 Answers

Is there a range for the random numbers? If you have a limit for random numbers and you keep generating unique random numbers, then you'll end up with a list of all numbers from x..y in random order, where x-y is the valid range of your random numbers. If this is the case, you might improve speed greatly by simply generating the list of all numbers x..y and shuffling it, instead of generating the numbers.

like image 62
jkramer Avatar answered Oct 02 '22 22:10

jkramer


I think there are 3 possible approaches, depending on range-size, and performance pattern needed you can use another algorithm.

  1. Create a random number, see if it is in (a sorted) list. If not add and return, else try another.
    • Your list will grow and consume memory with every number you need. If every number is 32 bit, it will grow with at least 32 bits every time.
    • Every new random number increases the hit-ratio and this will make it slower.
    • O(n^2) - I think
  2. Create an bit-array for every number in the range. Mark with 1/True if already returned.
    • Every number now only takes 1 bit, this can still be a problem if the range is big, but every number now only allocates 1 bit.
    • Every new random number increases the hit-ratio and this will make it slower.
    • O(n*2)
  3. Pre-populate a list with all the numbers, shuffle it, and return the Nth number.
    • The list will not grow, returning numbers will not get slower,
    • but generating the list might take a long time, and a lot of memory.
    • O(1)

Depending on needed speed, you could store all lists in a database. There's no need for them to be in memory except speed.

like image 33
GvS Avatar answered Oct 02 '22 21:10

GvS