# Generate random numbers between a range such that no number comes twice.

Sorry if my question is stupid, math has been always a wild beast for me. I am an application developer. In one application I have a module which assigns a random 6-8 digit number and a serial number to a manager. The manager can then give these two numbers to other people who uses them as credit on the application. Currently There are 134 managers and each manager can generate 50 codes per day.

To implement it I have a table that contains these records.

1. Serial No (starts from 1 and keeps on incrementing)
2. Authorization code (random number 6-8 digit. Non repetitive)
3. Manager code (code of manager who has generated it.)
4. Is Used? (1 if code has been used else 0)
5. Used on (date on which it was used so that we can delete this code so that authorization code can be reused.)

Whenever a manager tries to generate a code, the program creates a random number and see if this number is present or not in the table. If it is present, I regenerate or else I store it in the table.

So far so good, but the problem comes when many numbers are in use.

Currently there are 1090083 code in table. The total possible will be $99999999-999999 = 99000000$.

But at this very tiny amount only, I am having 2-8 collisions, i.e, the random number is already there and I have to generate another. This creats a lag and as the table grows it will soon become deamon.

According to me the problem lies with generation of numbers. Therefore I request you people to help me.

Is it possible to create a function on whose each ireatation a new non-repetitive number is outputed? So that I simply run it $n$ times to get $n$ numbers. Or any things else since you know the problem well.

-
do you have acces to the list of numbers that haven't been accessed? If you do you can pick a random number (n) (any) and choose as your non repeated number the one that is the nth largest. –  dREaM Jul 4 '13 at 16:11
no,since the requirement is to have only 6-8 digits number( due to compactibility issue with old system) which can not be guessed. –  Ratna Jul 4 '13 at 16:17
I do not know whether this is a solution but probably a trial worth. Have you tried to play with a "Linear congruential generator"? –  al-Hwarizmi Jul 4 '13 at 16:43
The standard build-in generators are robust enough that what are you describing should not happen (with 1% of usage), I would say that the problem lies with the seed of your generator or some similar issue. If you are worrying about speed that much, you could generate a random permutation of all the numbers and just go over that (stored somewhere) list, 8-digits numbers are hardly anything. –  dtldarek Jul 4 '13 at 16:55
You may perform binary search on the codes already generated. The lag then will be insignificant. –  Lord Soth Jul 4 '13 at 17:14

Most likely the random number generator you are using is either:

1. Seeded on something silly, and frequently begins at the same spot, or
2. Not capable of generating all 8 digit numbers, so you've actually already used up most of the random numbers your method can generate.

Both of these problems will be fixed if you use a better random number generator, such as the Mersenne twister, which may be included among your standard packages already.

-