0

I am working in php and I am trying to create 1000 tickets in a database. Each ticket needs it's own unique code that consists of letters and numbers about 6 characters long.

EXP.    
Tbl_Tickets
ID     code
1      3F2jk7
2      2HGUF1       
3      9FJDNJ
4      MFJEY9
5      23988D

I was wondering is there a simple way of doing this with php, or excel, or any other way for that matter. I know that i can use a random number generator, but the check for the Unique would have a large BigO notation and the check would get messy.

Sari Rahal
  • 1,897
  • 2
  • 32
  • 53

4 Answers4

0

Unique is not compatible with random, but the following might suit:

=CHOOSE(RANDBETWEEN(1,2),RANDBETWEEN(0,9),CHAR(RANDBETWEEN(65,90)))

copied across to populate six columns (say A to F) with, in G:

=A1&B1&C1&D1&E1&F1

and both copied down to say row 1100. Then select G, copy Paste Special Values, and Remove Duplicates on ColumnG and select first 1000 entries.

pnuts
  • 58,317
  • 11
  • 87
  • 139
0

You could easily create an array of strings in php and write it to a database:

 function generateRandomString($length = 6, $letters = '1234567890QWERTYUIOPASDFGHJKLZXCVBNM'){
    $s = '';
    $lettersLength = strlen($letters)-1;

    for($i = 0 ; $i < $length ; $i++){
        $s .= $letters[rand(0,$lettersLength)];
    }

    return $s;
} 
// Create an array with random strings
for ($i=0; $i<1000; $i++){
  $ticket_numbers = array();
  $ticket_number = generateRandomString();
  while (in_array($ticket_number,$ticket_numbers))
    $ticket_number = generateRandomString();
  $ticket_numbers[] = $ticket_number;
}
// Write the array to a database
$con = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error");
foreach ($ticket_numbers as $number){
  mysqli_query($con,"Your insert query using the value $number");
}
mysqli_close($con);

This should help you in the right direction though there are probably better ways to do this.

The function generateRandomString() was taken from How to generate random numbers/letters with PHP/Javascript

Community
  • 1
  • 1
bjarkig82
  • 558
  • 4
  • 13
0

And another option. Encryption is guaranteed to be unique, so encrypting the numbers 0, 1, 2, ... will give you guaranteed unique random-seeming output. Six characters is 30 bits using Base32, or 36 bits using Base64. You will need a 30 (or 36 bit) cypher. Unless you have a library that includes Hasty Pudding cypher (unlikely) then just implement a simple four round Feistel cypher with the appropriate block size. It will not be completely secure, but it will be enough to defeat casual attacks.

rossum
  • 15,344
  • 1
  • 24
  • 38
0

This will produce random strings in column B with no repeats from B1 thru B1001

Sub Lottery()
    Dim i As Long, j As Long, c As Collection
    Set c = New Collection
    v = Split("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
    For i = 1 To 5000
        can = ""
        For j = 1 To 6
            can = can & v(Application.RandBetween(0, 35))
        Next j
        On Error Resume Next
            c.Add can, CStr(can)
        On Error GoTo 0
        If c.Count = 1000 Then Exit For
    Next i

    For i = 1 To 1000
    Cells(i + 1, 2).Value = c(i)
    Next i
End Sub
Gary's Student
  • 95,722
  • 10
  • 59
  • 99