随机和随机问题 vb [英] random and shuffle questions vb

查看:21
本文介绍了随机和随机问题 vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从我的数据库中显示的问题进行混洗的逻辑感到困惑.我当前的代码向文本框显示随机问题,但它也显示空条目和重复条目.

i'm puzzled in the logic of shuffling the questions that i'll display from my database. my current code displays random questions to the textbox but it also display empty and duplicate entries.

    Dim r As MySqlDataReader
    Dim i As Integer = 0
    Dim temp() As Integer = {}


    Dim txtQ() As TextBox = {txtQ1, txtQ2, txtQ3, txtQ4, txtQ5, txtQ6, txtQ7, txtQ8, txtQ9, txtQ10}
    Dim cbA() As CheckedListBox = {cbA1, cbA2, cbA3, cbA4, cbA5, cbA6, cbA7, cbA8, cbA9, cbA10}

    con.Open()
    cmd = New MySqlCommand("select * from tbexam", con)
    r = cmd.ExecuteReader


    While r.Read
        If i <= 9 Then
            Randomize()
            Dim j As Integer = CInt(Int(9 * Rnd()))

            txtQ(j).Text = r.GetString("exam_question")
            cbA(j).Items.Clear()
            cbA(j).Items.Add("a) " & r.GetString("exam_ans_a"))
            cbA(j).Items.Add("b) " & r.GetString("exam_ans_b"))
            cbA(j).Items.Add("c) " & r.GetString("exam_ans_c"))
            cbA(j).Items.Add("d) " & r.GetString("exam_ans_d"))


            i = i + 1
        End If
    End While

推荐答案

当你在 j 中放置一个随机数时,它(非常接近)是一个介于 0 和 9 之间的随机数.这等效掷十面骰子.你认为如果你把它掷十次,你会得到 0 到 9 中的每一个值的概率是多少?相当低.你实际得到的是这样的:2, 0, 8, 2, 5, 1, 7, 6, 1, 4.这个序列包含2和1的重复,值3和9不会出现.这就是您的重复和差距的来源.

When you place a random number in j, it is (very nearly) a random number between 0 and 9. This is equivalent to rolling a ten-sided dice. What do you think the chances are that if you were to roll it ten times you would get exactly one of each of the values 0 to 9? Pretty low. What you will actually get is something like: 2, 0, 8, 2, 5, 1, 7, 6, 1, 4. This sequence contains duplicates of 2 and 1 and the values 3, and 9 do not appear. This is where your duplicates and gaps come from.

您可以尝试的一种选择是按顺序填充数组,然后对其进行洗牌.

One option you could try is to fill the array sequentially and then shuffle it.

Dim r As MySqlDataReader
Dim i As Integer = 0
Dim temp() As Integer = {}

Dim txtQ() As TextBox = {txtQ1, txtQ2, txtQ3, txtQ4, txtQ5, txtQ6, txtQ7, txtQ8, txtQ9, txtQ10}
Dim cbA() As CheckedListBox = {cbA1, cbA2, cbA3, cbA4, cbA5, cbA6, cbA7, cbA8, cbA9, cbA10}

con.Open()
cmd = New MySqlCommand("select * from tbexam", con)
r = cmd.ExecuteReader

' Read the Q&As into the arrays in the order they appear in the DB
While r.Read
    If i <= 9 Then
        txtQ(i).Text = r.GetString("exam_question")
        cbA(i).Items.Clear()
        cbA(i).Items.Add("a) " & r.GetString("exam_ans_a"))
        cbA(i).Items.Add("b) " & r.GetString("exam_ans_b"))
        cbA(i).Items.Add("c) " & r.GetString("exam_ans_c"))
        cbA(i).Items.Add("d) " & r.GetString("exam_ans_d"))
        i = i + 1
    End If
End While

' Re-order the arrays by swapping each element with another, randomly selected element.
' Start with i at the count of the elements read in the last loop and work down.
Dim n as Integer = i - 1
While i > 0
    Dim j As Integer = CInt(Int(n * Rnd()))
    i = i - 1
    ' Swap elements i and j of the arrays
    Dim tmpQ As TextBox = txtQ(i)
    txtQ(i) = txtQ(j)
    txtQ(j) = tmpQ
    Dim tmpA() As CheckedListBox = cbA(i)
    cbA(i) = cbA(j)
    cbA(j) = tmpA
End While

这可以简化和抽象,但你明白了.

This could be simplified and abstracted but you get the idea.

这篇关于随机和随机问题 vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆