For a multiplayer game I'm working on, I need to be able to create vehicles for players. Each vehicle needs to have an entirely unique license plate.
The vehicles details are stored in a MySQL table called "vehicles" and the license plate is in the "plate" column. I want to generate a random license plate (three alphanumeric characters, followed by a space, followed by three more alphanumeric characters) that doesn't match any in the table.
I'm not sure how to do it, though. I thought about using a while loop and just constantly regenerating a new plate until a SELECT query for that plate returns 0 rows but that just seems inefficient - is there a better way?
Your method should be fine. There are over 2 billion possible license plates in that scheme, and the database will only have a few thousand. So the chance of repeating a plate number will be very tiny. The first query will almost always confirm that the plate number is unique, and if not the second one will. If you ever had to do 3 queries, I would be very, very surprised.
The number of possible license plates is: 366 = 2,176,782,336