July 14th, 2002, 08:30 AM
random ID numbers
I'm making a system and I've found a problem that I didn't realize I had.
There's a tool in this system too add new users, blank users that will be filled later, this users are records in a mysql database and the ID of this users have to be random. Any idea of how can I make it ?
I mean, I have to alocate N (N provided by the user) random ID numbers which are inside a range K:L (K and L provided by the user) which are not in the database yet.
I think there wouldn't be any problem since they're specting about 2000 users and the comon range is from 1000 to 9999 (and the database supports more a really great range).
any tip ? any hint ?
PS: this will be done in PHP with MySQL
July 15th, 2002, 09:28 AM
One quick solution would be to have a table that holds all the ID numbers and a flag to show whether that ID has been used yet. If the user said he needed 2000 ID numbers between 1000 and 9999, you would populate the table with 2000 random numbers within that range. Each time an ID number was needed, you would select a number from the ID table whose "in use" flag hadn't been set yet and then set that "in use" flag to true for that ID number. You could also select the numbers from the table in a random order so that the random numbers in the table would not be used in sequential order. This would also lessen the chance of two users picking the same ID number from the table at the same time.