Generating random line from TXT without repeating

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alien8k
    New Member
    • Apr 2015
    • 3

    #1

    Generating random line from TXT without repeating

    Hello,
    first i want apologize for my english, but i hope u will undestand me.
    I have problem with me quiz program. Generating random 10questions (after 10 random Questions -> end) from txt without repeating, i sent u code, its work great, but questions are repeated.
    I have menu form where is start button, which will jump to form1 where is testing.. after 10 quesiton will show msgbox with result and jump back to menu.., testing form will hide as u can see. And every time when i start quiz i need every question just once.
    I dont know how fix it. May u help me please?
    My txt file:
    Question|Answer 1|Answer2|Answe r3|rightanswer( a/b/c)
    example:
    The capitol city of England is?|Prague|Sidn ey|New York|b
    this is syntax of my txt file.
    And i have this code:
    Code:
    Public Class Form1
        Dim q1(10), a1(10), a2(10), a3(10), ca(10) As String ' load questions , answers & correct answer
        Dim value As Integer = 0
        Dim counter As Integer = 0
        Dim x As Integer 'result
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label3.Text = Label3.Text + 1
    
            Dim selectedanswer As String = ""
    
            If RadioButton1.Checked = True Then selectedanswer = "a"
            If RadioButton2.Checked = True Then selectedanswer = "b"
            If RadioButton3.Checked = True Then selectedanswer = "c"
    
            If selectedanswer = ca(value) Then x = x + 1
            RadioButton1.Checked = False
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            If Label3.Text = 10 Then
                MsgBox("Your Result is " & x)
                Form2.Show()
                me.hide()
    
            End If
    
    
            loadquestions()
    
        End Sub
        Public Sub loadquestions()
    
    
            ' Initialize the random-number generator.
            Randomize()
            ' Generate random value between 1 and 10. 
            value = CInt(Int(((counter - 1) * Rnd()) + 1))
            Label1.Text = q1(value).ToString
            RadioButton1.Text = a1(value).ToString
            RadioButton2.Text = a2(value).ToString
            RadioButton3.Text = a3(value).ToString
    
        End Sub
    
        Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            OpenFileDialog1.ShowDialog()
    
    
    
    
            Dim soubor As New IO.StreamReader(OpenFileDialog1.FileName)
            While Not soubor.EndOfStream
                Dim txtline() As String = soubor.ReadLine().Split("|")
                q1(counter) = txtline(0)
                a1(counter) = txtline(1)
                a2(counter) = txtline(2)
                a3(counter) = txtline(3)
                ca(counter) = txtline(4)
                counter = counter + 1
            End While
            loadquestions()
        End Sub
    End Class
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to track the numbers already generated and each time you generate a new one, check it against that list until a new unused number is generated.

    Comment

    • alien8k
      New Member
      • Apr 2015
      • 3

      #3
      Can u give me an example please?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Of which piece? Storing a value? Or checking a value? Or generating numbers until a new one is generated?

        In psuedo code, you would need to do something like this:
        Code:
        arrayOfGeneratedNumbers(10)
        newNumberGenerated = False
        
        Do Until newNumberGenerated = True
           newNumberGenerated = True
           x = random number
           
           For Each y In arrayOfGeneratedNumbers
              If x = y Then
                 newNumberGenerated = False
              End If
           Next y
        Loop

        Comment

        • alien8k
          New Member
          • Apr 2015
          • 3

          #5
          like this?
          declared:
          y as int
          coutner as string
          value as string
          Code:
          Public Sub loadquestions()
          
                  Randomize()
                  value = CInt(Int(((counter - 1) * Rnd()) + 1))
          
          	arrayOfGeneratedNumbers(10)
          	newNumberGenerated = False
           
          	Do Until newNumberGenerated = True
            	newNumberGenerated = True
             	value = random number
           
             	For Each y In arrayOfGeneratedNumbers
                	If value = y Then
                  newNumberGenerated = False
                	End If
             		Next y
          		Loop
          
                  Label1.Text = q1(value).ToString
                  RadioButton1.Text = a1(value).ToString
                  RadioButton2.Text = a2(value).ToString
                  RadioButton3.Text = a3(value).ToString
          
          
              End Sub

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            My code was pseudo code, it's not meant to be used as is. You need the code to actually generate a random number for value. Not the words random number. You will also need to initiate the array to a number outside the range you want to generate. And once you generate a new distinct number, you need to store it in the array.

            All my pseudo code is doing is showing how you continue to generate numbers until you generate one that hasn't been used before from a known list of numbers. That is, with a loop and a check against an array.
            Last edited by Rabbit; Apr 20 '15, 04:07 PM.

            Comment

            Working...