Four algorithms of randomly sampled questions in the compilation of online examination system

Source: Internet
Author: User
Tags array sql range reference split table name variable
Algorithm | Random because of the need for teaching, I decided to write a asp+ms sql2000 online examination system, its function is mainly: to realize the judgment question, the single multiple-choice question and fill in the blank question on-line automatic answering, the change volume; and the student's wrong answer in the database, for the teacher analysis. In the preparation of questions from the test paper randomly extracted from the module of the algorithm, but quite a few twists and starts, the settlement process is recorded as follows, for your reference.

In order to illustrate the problem, the text provides the variable PD in the code to be extracted from the test question number, database table name and field name I have used Chinese, and only to judge the case.

Algorithm One

Because I do not know how to implement a random query from the question in the SQL statement, I downloaded a few free examination system on the Internet to study, found the first algorithm, the idea is to first read all the data in the database, get the total number of questions, generate a 1~ (the total number of questions-test number of questions) between the random number, and start reading the data here:

<% set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound=rs. Recordcount ' The total number of questions obtained

Randomize ' Initialize random number seed value

N=fix ((mycound-pd+1) *rnd+1)

Rs.move n ' pointer moves to n this random number this position

For I=1 to PD

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" ' Use session to record the standard answer

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<% Rs.movenext

Next

Rs.close%>

This algorithm can basically achieve the random sampling test, and let each student's questions and each refresh after the test questions are not the same, but it's the biggest problem is always the same sequence of questions, especially in the test questions are not many, students can almost use back answer method to cope with the exam. Although it is possible to change the order of the questions by changing the way the data is sorted, the changes are always small.

Algorithm two

The second algorithm is very simple, that is, to continuously generate the total number of questions in the 1~ of the random number, and then to the database to read this record, until the test to meet the amount of questions.

<%

Set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound=rs. Recordcount ' Get the total number of questions in the question bank

Rs.close

For I=1 to PD

Randomize

Sid=int ((Mycound + 1) *rnd+1) ' generates random numbers between the total number of questions in the 1~ question Bank

Set Rs=conn.execute ("SELECT * from Judgment question where id=" &sid)

While rs.eof

Randomize

Sid=int ((Mycound + 1) *rnd+1)

Set Rs=conn.execute ("SELECT * from Judgment question where id=" &sid) "If this question is not found in the database, continue to generate random number reading questions."

Wend

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" ' Use session to record the standard answer

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<%

Next

%>

This algorithm should be a real random sampling test, however, unfortunately, if the problem is not much in the question, it is easy to read the duplicate questions in the database, if you use a variable to store the question ID has been read to solve the problem of duplicate questions, the algorithm is too cumbersome, is very undesirable.

Algorithm two supplements:

The second algorithm is very simple, that is, to continuously generate the total number of questions in the 1~ of the random number, and then to the database to read this record, until the test to meet the amount of questions. At that time I thought that this algorithm should be a real random sampling test, however, unfortunately, if the problem is not much in the question, it is easy to read the duplicate questions in the database, although you can also use a variable or an array to store the question ID has been read to solve the problem of duplicate questions, the algorithm is too cumbersome. For this reason, I think it is undesirable to do so unilaterally. In fact, using a variable or an array to store the test ID has been read, the algorithm is not cumbersome.

<%

Write a function that generates random records

Function rndtest (m_count,r_count) ' parameter M_count for the total number of questions, r_count the number of questions to be read

Dim x,st,i

I=0

Do While I>=r_count

Randomize

X=fix (Rnd*m_count) +1 "produces 1~m_count random number

If not InStr (st,x) >0 Then

st=st&x& "," "use, split

I=i+1

End If

If I>=m_count then exit Do ' if M_count is less than r_count there will be a dead loop, so judge and jump out of the loop

Loop

Rndtest=st

End Function

Set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound =rndtest (Rs. Recordcount, PD) ' Get the total number of questions in the question bank

Testcound=split (Mycound, "'")

For i=0 to UBound (Testcound)

Rs.absoluteposition=matrix (i) ' Move the record pointer to the Testcound (i) record

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" Use session to record standard answers

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<%

Next

%>

Algorithm three

Because the second algorithm is easy to duplicate the question, in order to avoid the system to produce duplicate random number, I try to divide the total number of questions in the question bank into KP range, so that each range to produce a random number, so as to effectively avoid the random number of duplication.

<% set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound=rs. Recordcount ' The total number of questions obtained

For I=1 to PD

Randomize

Temp=fix (Fix (Rs. RECORDCOUNT/PD) +1 *rnd+1) ' Generate the total number of questions in the 1~ question number divided by the random number of test questions

Rs.move temp ' pointer to random number position

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" ' Use session to record the standard answer

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<%next

Rs.close%>

This algorithm can effectively solve the problem of algorithm one and two, not only to randomly extract the questions, but also to do the test questions do not repeat. But a careful thinking or there is a lack of: the question is that each of the questions in the test paper is not the same probability, so it appears unscientific. Because KP times all produce large number of probability is not big, so the line in the back of the examination of the chance to appear very small.

Algorithm Four

Algorithm four is my final research results, the algorithm is divided into three steps:

SETP1, get the total number of test questions, and then generate an array of the total number of 1~ questions.

SETP2 and generate random numbers to disrupt this matrix.

SETP3, sequentially remove the problem from the array.

This algorithm is similar to the principle of shuffling, as shown below:

(Set the total number of test database is 10, to extract 5 questions)

SETP1:

The initial contents of the array are as follows:

A1

A2

A3

A4

A5

A6

A7

A8

A9

A10

1

2

3

4

5

6

7

8

9

10

SETP2:

Generates two random numbers, such as 3 and 6. The contents of the A3 and A6 are then exchanged, and the contents of the array become:

a1

a2

a3

a4

a5

a6

a7

a8

a9

a10

1

2

6

4

5

3

7

8

9

10

SETP3, in order to remove the array of topics A1~A5 content, should be 1, 2, 6, 4, 5, read the corresponding questions in the database.

If the cyclic SETP2 is repeated, the contents of the array are randomly disturbed, so that both the objective of random sampling and the duplication of test questions are avoided.

<%

Dim matrix () defines an array of variables

Set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound=rs. Recordcount ' The total number of questions obtained

' Set the initial value of the array

For I=0 to Mycound

Matrix (i) =i+1

Next

Randomize ' generates random number of seeds

For i=0 to 2*mycound ' loops 2* questions total number of times

J=fix (Rnd*mycound)

K=fix (Rnd*mycound)

' Exchange of Matrix (k) and Matrix (j) Content

Temp=matrix (k)

Matrix (k) =matrix (j)

Matrix (j) =temp

Next

' Take out the problem in the array as the number of questions in the quiz paper

For I=1 to PD

Rs.absoluteposition=matrix (i) ' Move the record pointer to the matrix (i) record

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" ' Use session to record the standard answer

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<%next

Rs.close%>

Algorithm four supplements:

The definition of a large enough array was not resolved at that time, and the Dim matrix (x) was used to preset a large enough number X. Although there is no problem at the moment, but if the number of questions in the database is very large? Always like a stone in my heart. Oh. Now that the ReDim statement is found, the VBS reference describes the ReDim statement by declaring the dynamic array variable at the procedure level and allocating or reallocating the storage space. This is done using the ReDim matrix (RST). Recordcount) will be able to solve the problem perfectly.

<%

Dim Matrix ()

Set Rs=server. CreateObject ("ADODB.") RecordSet ")

Sql= "SELECT * from judgment question ORDER by ID ASC"

Rs.Open sql,conn,1,1

Mycound=rs. Recordcount ' The total number of questions obtained

ReDim Matrix (rst. Recordcount) ' defines one equal to rst. Array of RecordCount

' Set the initial value of the array

For I=0 to Mycound

Matrix (i) =i+1

Next

Randomize ' generates random number of seeds

For i=0 to 2*mycound ' loops 2* questions total number of times

J=fix (Rnd*mycound)

K=fix (Rnd*mycound)

' Exchange of Matrix (k) and Matrix (j) Content

Temp=matrix (k)

Matrix (k) =matrix (j)

Matrix (j) =temp

Next

' Take out the problem in the array as the number of questions in the quiz paper

For I=1 to PD

Rs.absoluteposition=matrix (i) ' Move the record pointer to the matrix (i) record

Session ("Pdda") =session ("Pdda") &rs ("correct answer") & "|" Use session to record standard answers

' Output questions and answers%>

<tr>

<TD width= "10%" ><%=i%>, <%=rs ("topic content")%></td>

<TD align= "center" width= "10%" ><select name= "cate<%=i%>" >

<option selected value=true> to </option>

<option value=false> wrong </option></select> </td>

</tr>

<%next

Rs.close%>

Summarize:

In contrast, algorithm four should be the most reasonable. But I do not know how many times the SETP2 should be cycled, the number in the array is the most "random", and I do not know when the question library of the number of questions in the case, this algorithm will be very much of the system resources, welcome to write and I discuss.



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.