Here is an example of how to generate such a voucher number:
$voucher_number = substr(md5(uniqid(mt_rand(), true)), 0, 6);
The database handling code depends on your database management system and database API, but in pseudocode:
Do
Generate voucher number
Insert voucher details
While there is an error and the error is a unique constraint violation
Note that will only a million possible voucher numbers, if you attempt to give out more than a million vouchers, the loop will become an infinite loop.