Data | database | random | repeat
See people often ask this question. and went to the internet to find some look, feel not satisfied with the writing.
Most of the readings are recorded into the array to extract. Yesterday and saw people write, whim on the right to think of a, personally think this method should be wrong, hehe. Just 20 lines ~_~
Implementation method:
1. Move to a random location in the open recordset to read the record through the removable feature of the recordset cursor. You can also use the RS object that is already open on the page.
2. Saves the random number that appears in a string, checks for repetition by the InStr function, and repeats the recursive call function to no repetition.
Database open Query Some, omit ... Set rs = Server.CreateObject ("Adodb.recordset") Rs.Open Sql,conn,1,1dim appearedcall Disrndrecord (10,rs.recordCount) ' Call function This position displays the record ' ############### #SUBS ################ ' #DisRndRecord (disnum,rsbound) ' #参数DisNum: Show Number ' # Parameter rsbound: Random number Generation range sub Disrndrecord (disnum,rsbound) DIM i,thisrnd If Rsbound < Disnum Then = Disnum ' Total records less than to extract a record bar Number of cases for i = 0 to DisNum-1 thisrnd = getrnd (rsbound) ' Get a repeat random number Rs. Move (THISRND) ' cursor to the number of random number positions read Response.Write ("<br>" &rs ("id") & ")" &rs ("Title") Rs. Move (-thisrnd) nextend Sub ' # function getrnd (bound) returns a distinct random number ' #参数bound: Random range function getrnd (bound) DIM rannum Randomize () Rannum=int (BOUND*RND) If Instr (appeared, "[" &ranNum&] ") Then ' the random number produced has not occurred rannum = getrnd (bound) End If appeared = appeared & "[&ranNum&]" ' record random number that has occurred getrnd = Rannumend Function