创建独特的随机字符串

问题描述:

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.

我在php工作,我正在尝试在数据库中创建1000张票。 每张票都需要它自己的唯一代码,该代码由大约6个字符长的字母和数字组成。 p>

  EXP。  
Tbl_Tickets 
ID代码
1 3F2jk7 
2 2HGUF1 
3 9FJDNJ 
4 MFJEY9 
5 23988D 
  code>  pre> 
 
 

我想知道有没有一种简单的方法用PHP执行此操作 ,或者excel,或任何其他方式。 我知道我可以使用随机数生成器,但是对Unique的检查会有一个大的BigO表示法,并且检查会变得混乱。 p> div>

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.

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

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.

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