Simple and efficient: Do not repeat random read database records

Source: Internet
Author: User
Tags range repetition
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.