Two days ago, I saw someone had to compile an examination system. At that time, I simply went back and used the random function RND.
Generally, N questions need to be randomly extracted from the database.
BelowCodeAll are based on vbs;
This is usually the case for writing similar
'Generate random numbers that are not repeated Function rndarray (istart, iend, sum) Dim arrayid (), I, J, blnre, temp, iloop, eloop Redim arrayid (Sum-1) I = 0 Iloop = 0 Eloop = 0 Blnre = false Randomize Do while I <sum Temp = int (RND * (iend-istart + 1) + istart) If I = 0 then Arrayid (0) = temp I = I + 1 Iloop = iloop + 1 Else For J = 0 to I-1 If arrayid (j) = temp then Blnre = true Iloop = iloop + 1 Exit for 'is very important to prevent unnecessary loops. Else Iloop = iloop + 1 End if Next If blnre = false then Arrayid (I) = temp I = I + 1 Else Blnre = false End if End if Eloop = eloop + iloop Iloop = 0 Loop Rndarray = join (arrayid) & "number of cycles:" & eloop End Function Response. Write rndarray (, 5) & "<br>" 'Call Process |
PS. Iloop and eloop are used to calculate the number of cycles.
Most people write this method to generate a random number, and then compare it with the previous one to determine whether it is available;
But this is not an AI, or an efficient method. Why not use two arrays?
Array 1: stores the required string, number, and so on. array 2 stores the generated random number. Each time a subscript X of the intermediate variable temp is randomly generated, it is assigned to array 2, then, remove the number with the subscript X from array 1 and assign it to the intermediate variable temp. In this way, every random number generated will be removed from array 1, the next generation of a number will not repeat. The principle of this random number generation method is actually extracted from array 1.
Method 2
Function rndstr (istart, iend, isum) Dim I, j, vntarray () Redim vntarray (iend-istart) J = istart For I = 0 to iend-istart Vntarray (I) = J J = J + 1 Next Dim vntarray2 (), temp, X, Y Redim vntarray2 (isum-1) Y = iend-istart + 1 X = 0 Temp = vntarray Do while x <isum Dim Randomize Vntarray2 (x) = temp (INT (RND * y )) A = "" & vntarray2 (x )&"" Temp = Split (TRIM (replace (CHR (32) & join (temp) & CHR (32), ,""))) X = x + 1 Y = Y-1 Loop Rndstr = join (vntarray2) End Function Response. Write rndstr (1, 5, 2) |
Is it simpler?
Expand. If you want to generate a random string that contains letters and numbers, use the CHR (Num) function to assign a value to array 1 );
Assume that you need to create a page for winning mobile phonesProgram.
First, assign the value to array 1, which can be 130 ....~ 139... loop assignment, of course, in actual use, the existing assignment is assigned to the database, and then randomly extracted and assigned to array 2;
Finally, modify it.
Temp = Replace (join (array2), CHR (32 ),"") Phone = left (temp, 6) & "***" & right (temp, 2) |
The result is similar to 137648 *** 58.
So many writes, exhausted ~~