Home » Mysql » Generating a random & unique 8 character string using MySQL

Generating a random & unique 8 character string using MySQL

Posted by: admin November 1, 2017 Leave a comment

Questions:

I’m working on a game which involves vehicles at some point. I have a MySQL table named “vehicles” containing the data about the vehicles, including the column “plate” which stores the License Plates for the vehicles.

Now here comes the part I’m having problems with. I need to find an unused license plate before creating a new vehicle – it should be an alphanumeric 8-char random string. How I achieved this was using a while loop in Lua, which is the language I’m programming in, to generate strings and query the DB to see if it is used. However, as the number of vehicles increases, I expect this to become even more inefficient it is right now. Therefore, I decided to try and solve this issue using a MySQL query.

The query I need should simply generate a 8-character alphanumeric string which is not already in the table. I thought of the generate&check loop approach again, but I’m not limiting this question to that just in case there’s a more efficient one. I’ve been able to generate strings by defining a string containing all the allowed chars and randomly substringing it, and nothing more.

Any help is appreciated.

Answers:

This problem consists of two very different sub-problems:

  • the string must be seemingly random
  • the string must be unique

While randomness is quite easily achieved, the uniqueness without a retry loop is not. This brings us to concentrate on the uniqueness first. Non-random uniqueness can trivially be achieved with AUTO_INCREMENT. So using a uniqueness-preserving, pseudo-random transformation would be fine:

  • Hash has been suggested by @paul
  • AES-encrypt fits also
  • But there is a nice one: RAND(N) itself!

A sequence of random numbers created by the same seed is guaranteed to be

  • reproducible
  • different for the first 8 iterations
  • if the seed is an INT32

So we use @AndreyVolk’s or @GordonLinoff’s approach, but with a seeded RAND:

e.g. Assumin id is an AUTO_INCREMENT col:

INSERT INTO vehicles VALUES (blah); -- leaving out the number plate
SELECT @lid:=LAST_INSERT_ID();
UPDATE vehicles SET numberplate=concat(
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed)*36+1, 1)
             )
     WHERE [email protected];

Questions:
Answers:

What about calculating the MD5 (or other) hash of sequential integers, then taking the first 8 characters.

i.e

MD5(1) = c4ca4238a0b923820dcc509a6f75849b => c4ca4238
MD5(2) = c81e728d9d4c2f636f067f89cc14862c => c81e728d
MD5(3) = eccbc87e4b5ce2fe28308fd9f2a7baf3 => eccbc87e

etc.

caveat: I have no idea how many you could allocate before a collision (but it would be a known and constant value).

edit: This is now an old answer, but I saw it again with time on my hands, so, from observation…

Chance of all numbers = 2.35%

Chance of all letters = 0.05%

First collision when MD5(82945) = “7b763dcb…” (same result as MD5(25302))

Questions:
Answers:

As I stated in my comment, I woudn’t bother with the likelihood of collision. Just generate a random string and check if it exists. If it does, try again and you shouldn’t need to do it more that a couple of times unless you have a huge number of plates already assigned.

Another solution for generating an 8-character long pseudo-random string in pure (My)SQL:

SELECT LEFT(UUID(), 8);

You can try the following (pseudo-code):

DO 
    SELECT LEFT(UUID(), 8) INTO @plate;
    INSERT INTO plates (@plate);
WHILE there_is_a_unique_constraint_violation
-- @plate is your newly assigned plate number

Questions:
Answers:

Here is one way, using alpha numerics as valid characters:

select concat(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1)
             ) as LicensePlaceNumber;

Note there is no guarantee of uniqueness. You’ll have to check for that separately.

Questions:
Answers:

You may use MySQL’s rand() and char() function:

select concat( 
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97),
    char(round(rand()*25)+97)
) as name;

Questions:
Answers:

Create a random string

Here’s a MySQL function to create a random string of a given length.

DELIMITER $$

CREATE DEFINER=`root`@`%` FUNCTION `RandString`(length SMALLINT(3)) RETURNS varchar(100) CHARSET utf8
begin
    SET @returnStr = '';
    SET @allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    SET @i = 0;

    WHILE (@i < length) DO
        SET @returnStr = CONCAT(@returnStr, substring(@allowedChars, FLOOR(RAND() * LENGTH(@allowedChars) + 1), 1));
        SET @i = @i + 1;
    END WHILE;

    RETURN @returnStr;
END

Usage SELECT RANDSTRING(8) to return an 8 character string.

You can customize the @allowedChars.

Uniqueness isn’t guaranteed – as you’ll see in the comments to other solutions, this just isn’t possible. Instead you’ll need to generate a string, check if it’s already in use, and try again if it is.


Check if the random string is already in use

If we want to keep the collision checking code out of the app, we can create a trigger:

DELIMITER $$

CREATE TRIGGER Vehicle_beforeInsert
  BEFORE INSERT ON `Vehicle`
  FOR EACH ROW
  BEGIN
    SET @vehicleId = 1;
    WHILE (@vehicleId IS NOT NULL) DO 
      SET NEW.plate = RANDSTRING(8);
      SET @vehicleId = (SELECT id FROM `Vehicle` WHERE `plate` = NEW.plate);
    END WHILE;
  END;$$
DELIMITER ;

Questions:
Answers:

You can generate a random alphanumeric string with:

lpad(conv(floor(rand()*pow(36,8)), 10, 36), 8, 0);

You can use it in a BEFORE INSERT trigger and check for a duplicate in a while loop:

CREATE TABLE `vehicles` (
    `plate` CHAR(8) NULL DEFAULT NULL,
    `data` VARCHAR(50) NOT NULL,
    UNIQUE INDEX `plate` (`plate`)
);

DELIMITER //
CREATE TRIGGER `vehicles_before_insert` BEFORE INSERT ON `vehicles`
FOR EACH ROW BEGIN

    declare str_len int default 8;
    declare ready int default 0;
    declare rnd_str text;
    while not ready do
        set rnd_str := lpad(conv(floor(rand()*pow(36,str_len)), 10, 36), str_len, 0);
        if not exists (select * from vehicles where plate = rnd_str) then
            set new.plate = rnd_str;
            set ready := 1;
        end if;
    end while;

END//
DELIMITER ;

Now just insert your data like

insert into vehicles(col1, col2) values ('value1', 'value2');

And the trigger will generate a value for the plate column.

(sqlfiddle demo)

That works this way if the column allows NULLs. If you want it to be NOT NULL you would need to define a default value

`plate` CHAR(8) NOT NULL DEFAULT 'default',

You can also use any other random string generating algorithm in the trigger if uppercase alphanumerics isn’t what you want. But the trigger will take care of uniqueness.

Questions:
Answers:

I Use data from another column to generate a “hash” or unique string

UPDATE table_name SET column_name = Right( MD5(another_column_with_data), 8 )

Questions:
Answers:

If you’re OK with “random” but entirely predictable license plates, you can use a linear-feedback shift register to choose the next plate number – it’s guaranteed to go through every number before repeating. However, without some complex math, you won’t be able to go through every 8 character alphanumeric string (you’ll get 2^41 out of the 36^8 (78%) possible plates). To make this fill your space better, you could exclude a letter from the plates (maybe O), giving you 97%.

Questions:
Answers:

Taking into account the total number of characters that you require, you would have a very small chance of generating two exactly similar number plates. Thus you could probably get away with generating the numbers in LUA.

You have 36^8 different unique numberplates (2,821,109,907,456, that’s a lot), even if you already had a million numberplates already, you’d have a very small chance of generating one you already have, about 0.000035%

Of course, it all depends on how many numberplates you will end up creating.

Questions:
Answers:

8 letters from the alphabet – All caps:

UPDATE `tablename` SET `tablename`.`randomstring`= concat(CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25)))CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25))),CHAR(FLOOR(65 + (RAND() * 25))));

Questions:
Answers:

For a String consisting of 8 random numbers and upper- and lowercase letters, this is my solution:

LPAD(LEFT(REPLACE(REPLACE(REPLACE(TO_BASE64(UNHEX(MD5(RAND()))), "/", ""), "+", ""), "=", ""), 8), 8, 0)

Explained from inside out:

  1. RAND generates a random number between 0 and 1
  2. MD5 calculates the MD5 sum of (1), 32 characters from a-f and 0-9
  3. UNHEX translates (2) into 16 bytes with values from 00 to FF
  4. TO_BASE64 encodes (3) as base64, 22 characters from a-z and A-Z and 0-9 plus “/” and “+”, followed by two “=”
  5. the three REPLACEs remove the “/”, “+” and “=” characters from (4)
  6. LEFT takes the first 8 characters from (5), change 8 to something else if you need more or less characters in your random string
  7. LPAD inserts zeroes at the beginning of (6) if it is less than 8 characters long; again, change 8 to something else if needed
Questions:
Answers:

If you dont have a id or seed, like its its for a values list in insert:

REPLACE(RAND(), '.', '')