Results 1 to 14 of 14

Thread: Randomize Questions and Answers

Hybrid View

  1. #1
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Randomize Questions and Answers

    A beginning generalized approach for asking questions and answers. You'll need a button and rich textbox to try this.

    Code:
        Private Shared PRNG As New Random
        Private Qorder As List(Of Integer)
        Private Correct As String
        Private Shared Answers As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Private Sub btnNxtQ_Click(sender As Object, e As EventArgs) Handles btnNxtQ.Click
            'need questions?
            If Qorder Is Nothing OrElse Qorder.Count = 0 Then
                'get Q's in random order
                Qorder = Enumerable.Range(0, QA.Elements.Count).OrderBy(Function(r) PRNG.Next).ToList
            End If
            'get a Q
            Dim q As XElement = QA.Elements.Skip(Qorder(0)).Take(1).First
            Qorder.RemoveAt(0) 'remove it
            'show Q
            RichTextBox1.Clear()
            RichTextBox1.AppendText(q.<q>.First.Value)
            RichTextBox1.AppendText(ControlChars.Cr)
            RichTextBox1.AppendText(ControlChars.Cr)
            'then show answers
            Dim ansIDX As Integer = 0
            Correct = ""
    
            For Each a As XElement In q...<a>.OrderBy(Function(r) PRNG.Next) 'note answers in random order
                RichTextBox1.AppendText(Answers(ansIDX) & ")  ") 'show letter
                RichTextBox1.AppendText(a.Value) 'then answer
                RichTextBox1.AppendText(ControlChars.Cr)
                If a.@c = "y" Then 'correct?
                    Correct = Answers(ansIDX) 'yes
                End If
                ansIDX += 1
            Next
            'for debug
            If Correct = "" Then Stop 'should not happen
            'RichTextBox1.AppendText(ControlChars.Cr)
            'RichTextBox1.AppendText(ControlChars.Cr)
            'RichTextBox1.AppendText(Correct)
        End Sub
    
        'questions and answers
        Private QA As XElement = <questions>
                                     <!-- question answer
                                            contained with an aq element
                                            which has ONE q element that is the question
                                            and as many a elements as desired
                                            the a element that is the correct answer should have a c attribute= y -->.
                                     <aq>
                                         <q>How many states in the US?</q>
                                         <a>41</a>
                                         <a>47</a>
                                         <a>49</a>
                                         <a c="y">50</a>
                                     </aq>
                                     <aq>
                                         <q>How many planets in our solar system?</q>
                                         <a>7</a>
                                         <a c="y">8</a>
                                         <a>9</a>
                                     </aq>
                                     <aq>
                                         <q>How far is it to the moon(in miles)?</q>
                                         <a>9,000</a>
                                         <a>363,104</a>
                                         <a c="y">238,855</a>
                                         <a>123,000</a>
                                         <a>93,000,000</a>
                                     </aq>
                                     <aq>
                                         <a>F</a>
                                         <a c="y">T</a>
                                         <q>Jefferson City is Missouri's capital?</q>
                                     </aq>
                                     <aq>
                                         <a>Little Rock</a>
                                         <a>Trantor</a>
                                         <q>The capital of Alaska is?</q>
                                         <a>Metropolis</a>
                                         <a c="y">Juneau</a>
                                         <a>Gotham City</a>
                                     </aq>
                                 </questions>

    States Q's that can be substituted for QA in the above code. Manipulate the answers as needed.

    Code:
    <questions>
      <aq>
        <q>The capital of Alabama is?</q>
        <a c="y">Montgomery</a>
        <a>Harrisburg</a>
        <a>Olympia</a>
        <a>Charleston</a>
        <a>Providence</a>
      </aq>
      <aq>
        <q>The capital of Alaska is?</q>
        <a c="y">Juneau</a>
        <a>Tallahassee</a>
        <a>Augusta</a>
        <a>Dover</a>
        <a>Salem</a>
      </aq>
      <aq>
        <q>The capital of Arizona is?</q>
        <a c="y">Phoenix</a>
        <a>Charleston</a>
        <a>Denver</a>
        <a>Little Rock</a>
        <a>Helena</a>
      </aq>
      <aq>
        <q>The capital of Arkansas is?</q>
        <a c="y">Little Rock</a>
        <a>Lansing</a>
        <a>Jefferson City</a>
        <a>Dover</a>
        <a>Montpelier</a>
      </aq>
      <aq>
        <q>The capital of California is?</q>
        <a c="y">Sacramento</a>
        <a>Oklahoma City</a>
        <a>Lincoln</a>
        <a>Dover</a>
        <a>Atlanta</a>
      </aq>
      <aq>
        <q>The capital of Colorado is?</q>
        <a c="y">Denver</a>
        <a>Lansing</a>
        <a>Frankfort</a>
        <a>Albany</a>
        <a>Olympia</a>
      </aq>
      <aq>
        <q>The capital of Connecticut is?</q>
        <a c="y">Hartford</a>
        <a>Santa Fe</a>
        <a>Olympia</a>
        <a>Indianapolis</a>
        <a>Montpelier</a>
      </aq>
      <aq>
        <q>The capital of Delaware is?</q>
        <a c="y">Dover</a>
        <a>Madison</a>
        <a>Atlanta</a>
        <a>Richmond</a>
        <a>Indianapolis</a>
      </aq>
      <aq>
        <q>The capital of Florida is?</q>
        <a c="y">Tallahassee</a>
        <a>Springfield</a>
        <a>Honolulu</a>
        <a>Augusta</a>
        <a>Cheyenne</a>
      </aq>
      <aq>
        <q>The capital of Georgia is?</q>
        <a c="y">Atlanta</a>
        <a>Baton Rouge</a>
        <a>Madison</a>
        <a>Denver</a>
        <a>Austin</a>
      </aq>
      <aq>
        <q>The capital of Hawaii is?</q>
        <a c="y">Honolulu</a>
        <a>Frankfort</a>
        <a>Columbus</a>
        <a>Topeka</a>
        <a>Annapolis</a>
      </aq>
      <aq>
        <q>The capital of Idaho is?</q>
        <a c="y">Boise</a>
        <a>Des Moines</a>
        <a>Topeka</a>
        <a>Montpelier</a>
        <a>Madison</a>
      </aq>
      <aq>
        <q>The capital of Illinois is?</q>
        <a c="y">Springfield</a>
        <a>Augusta</a>
        <a>Boston</a>
        <a>Nashville</a>
        <a>Lincoln</a>
      </aq>
      <aq>
        <q>The capital of Indiana is?</q>
        <a c="y">Indianapolis</a>
        <a>Denver</a>
        <a>Jefferson City</a>
        <a>Sacramento</a>
        <a>Montpelier</a>
      </aq>
      <aq>
        <q>The capital of Iowa is?</q>
        <a c="y">Des Moines</a>
        <a>Baton Rouge</a>
        <a>Topeka</a>
        <a>Saint Paul</a>
        <a>Honolulu</a>
      </aq>
      <aq>
        <q>The capital of Kansas is?</q>
        <a c="y">Topeka</a>
        <a>Madison</a>
        <a>Denver</a>
        <a>Olympia</a>
        <a>Helena</a>
      </aq>
      <aq>
        <q>The capital of Kentucky is?</q>
        <a c="y">Frankfort</a>
        <a>Albany</a>
        <a>Lincoln</a>
        <a>Baton Rouge</a>
        <a>Madison</a>
      </aq>
      <aq>
        <q>The capital of Louisiana is?</q>
        <a c="y">Baton Rouge</a>
        <a>Providence</a>
        <a>Bismarck</a>
        <a>Augusta</a>
        <a>Columbus</a>
      </aq>
      <aq>
        <q>The capital of Maine is?</q>
        <a c="y">Augusta</a>
        <a>Oklahoma City</a>
        <a>Salt Lake City</a>
        <a>Frankfort</a>
        <a>Honolulu</a>
      </aq>
      <aq>
        <q>The capital of Maryland is?</q>
        <a c="y">Annapolis</a>
        <a>Dover</a>
        <a>Concord</a>
        <a>Trenton</a>
        <a>Harrisburg</a>
      </aq>
      <aq>
        <q>The capital of Massachusetts is?</q>
        <a c="y">Boston</a>
        <a>Hartford</a>
        <a>Boise</a>
        <a>Atlanta</a>
        <a>Salt Lake City</a>
      </aq>
      <aq>
        <q>The capital of Michigan is?</q>
        <a c="y">Lansing</a>
        <a>Juneau</a>
        <a>Montgomery</a>
        <a>Hartford</a>
        <a>Frankfort</a>
      </aq>
      <aq>
        <q>The capital of Minnesota is?</q>
        <a c="y">Saint Paul</a>
        <a>Indianapolis</a>
        <a>Honolulu</a>
        <a>Juneau</a>
        <a>Salem</a>
      </aq>
      <aq>
        <q>The capital of Mississippi is?</q>
        <a c="y">Jackson</a>
        <a>Salem</a>
        <a>Honolulu</a>
        <a>Augusta</a>
        <a>Sacramento</a>
      </aq>
      <aq>
        <q>The capital of Missouri is?</q>
        <a c="y">Jefferson City</a>
        <a>Lincoln</a>
        <a>Indianapolis</a>
        <a>Juneau</a>
        <a>Dover</a>
      </aq>
      <aq>
        <q>The capital of Montana is?</q>
        <a c="y">Helena</a>
        <a>Augusta</a>
        <a>Nashville</a>
        <a>Raleigh</a>
        <a>Bismarck</a>
      </aq>
      <aq>
        <q>The capital of Nebraska is?</q>
        <a c="y">Lincoln</a>
        <a>Columbus</a>
        <a>Columbia</a>
        <a>Raleigh</a>
        <a>Tallahassee</a>
      </aq>
      <aq>
        <q>The capital of Nevada is?</q>
        <a c="y">Carson City</a>
        <a>Montpelier</a>
        <a>Pierre</a>
        <a>Springfield</a>
        <a>Raleigh</a>
      </aq>
      <aq>
        <q>The capital of New Hampshire is?</q>
        <a c="y">Concord</a>
        <a>Madison</a>
        <a>Raleigh</a>
        <a>Annapolis</a>
        <a>Cheyenne</a>
      </aq>
      <aq>
        <q>The capital of New Jersey is?</q>
        <a c="y">Trenton</a>
        <a>Jackson</a>
        <a>Montpelier</a>
        <a>Boston</a>
        <a>Honolulu</a>
      </aq>
      <aq>
        <q>The capital of New Mexico is?</q>
        <a c="y">Santa Fe</a>
        <a>Denver</a>
        <a>Springfield</a>
        <a>Montgomery</a>
        <a>Jackson</a>
      </aq>
      <aq>
        <q>The capital of New York is?</q>
        <a c="y">Albany</a>
        <a>Oklahoma City</a>
        <a>Atlanta</a>
        <a>Olympia</a>
        <a>Concord</a>
      </aq>
      <aq>
        <q>The capital of North Carolina is?</q>
        <a c="y">Raleigh</a>
        <a>Saint Paul</a>
        <a>Montgomery</a>
        <a>Montpelier</a>
        <a>Salem</a>
      </aq>
      <aq>
        <q>The capital of North Dakota is?</q>
        <a c="y">Bismarck</a>
        <a>Columbia</a>
        <a>Topeka</a>
        <a>Albany</a>
        <a>Saint Paul</a>
      </aq>
      <aq>
        <q>The capital of Ohio is?</q>
        <a c="y">Columbus</a>
        <a>Columbia</a>
        <a>Montgomery</a>
        <a>Denver</a>
        <a>Des Moines</a>
      </aq>
      <aq>
        <q>The capital of Oklahoma is?</q>
        <a c="y">Oklahoma City</a>
        <a>Nashville</a>
        <a>Jefferson City</a>
        <a>Raleigh</a>
        <a>Helena</a>
      </aq>
      <aq>
        <q>The capital of Oregon is?</q>
        <a c="y">Salem</a>
        <a>Springfield</a>
        <a>Nashville</a>
        <a>Baton Rouge</a>
        <a>Columbus</a>
      </aq>
      <aq>
        <q>The capital of Pennsylvania is?</q>
        <a c="y">Harrisburg</a>
        <a>Providence</a>
        <a>Raleigh</a>
        <a>Lincoln</a>
        <a>Bismarck</a>
      </aq>
      <aq>
        <q>The capital of Rhode Island is?</q>
        <a c="y">Providence</a>
        <a>Dover</a>
        <a>Little Rock</a>
        <a>Columbus</a>
        <a>Topeka</a>
      </aq>
      <aq>
        <q>The capital of South Carolina is?</q>
        <a c="y">Columbia</a>
        <a>Springfield</a>
        <a>Annapolis</a>
        <a>Albany</a>
        <a>Jackson</a>
      </aq>
      <aq>
        <q>The capital of South Dakota is?</q>
        <a c="y">Pierre</a>
        <a>Hartford</a>
        <a>Augusta</a>
        <a>Lincoln</a>
        <a>Denver</a>
      </aq>
      <aq>
        <q>The capital of Tennessee is?</q>
        <a c="y">Nashville</a>
        <a>Annapolis</a>
        <a>Cheyenne</a>
        <a>Boise</a>
        <a>Tallahassee</a>
      </aq>
      <aq>
        <q>The capital of Texas is?</q>
        <a c="y">Austin</a>
        <a>Phoenix</a>
        <a>Nashville</a>
        <a>Lansing</a>
        <a>Little Rock</a>
      </aq>
      <aq>
        <q>The capital of Utah is?</q>
        <a c="y">Salt Lake City</a>
        <a>Helena</a>
        <a>Raleigh</a>
        <a>Carson City</a>
        <a>Des Moines</a>
      </aq>
      <aq>
        <q>The capital of Vermont is?</q>
        <a c="y">Montpelier</a>
        <a>Austin</a>
        <a>Frankfort</a>
        <a>Little Rock</a>
        <a>Dover</a>
      </aq>
      <aq>
        <q>The capital of Virginia is?</q>
        <a c="y">Richmond</a>
        <a>Helena</a>
        <a>Pierre</a>
        <a>Santa Fe</a>
        <a>Montgomery</a>
      </aq>
      <aq>
        <q>The capital of Washington is?</q>
        <a c="y">Olympia</a>
        <a>Nashville</a>
        <a>Baton Rouge</a>
        <a>Des Moines</a>
        <a>Helena</a>
      </aq>
      <aq>
        <q>The capital of West Virginia is?</q>
        <a c="y">Charleston</a>
        <a>Nashville</a>
        <a>Juneau</a>
        <a>Austin</a>
        <a>Des Moines</a>
      </aq>
      <aq>
        <q>The capital of Wisconsin is?</q>
        <a c="y">Madison</a>
        <a>Charleston</a>
        <a>Hartford</a>
        <a>Montpelier</a>
        <a>Lincoln</a>
      </aq>
      <aq>
        <q>The capital of Wyoming is?</q>
        <a c="y">Cheyenne</a>
        <a>Raleigh</a>
        <a>Salt Lake City</a>
        <a>Springfield</a>
        <a>Des Moines</a>
      </aq>
    </questions>
    This code, using your arrays, generated the above,

    Code:
            Dim aq As XElement = <questions></questions>
    
            For idx As Integer = 0 To questions.Length - 1
    
                Dim q As XElement = <aq>
                                        <q>The capital of <%= questions(idx).Trim("?"c) %> is?</q>
                                        <a c="y"><%= answers(idx) %></a>
                                    </aq>
                
                Dim ie As List(Of String)
                ie = (From s In answers Where s <> answers(idx) Select s Order By PRNG.Next).ToList
    
                For Each s As String In ie.Take(4)
                    Dim a As XElement = <a><%= s %></a>
                    q.Add(a)
                Next
                aq.Add(q)
            Next
    Last edited by dbasnett; Jan 14th, 2022 at 10:58 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width