473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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|Answer1|Answer2|Answer3|rightanswer(a/b/c)
example:
The capitol city of England is?|Prague|Sidney|New York|b
this is syntax of my txt file.
And i have this code:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim q1(10), a1(10), a2(10), a3(10), ca(10) As String ' load questions , answers & correct answer
  3.     Dim value As Integer = 0
  4.     Dim counter As Integer = 0
  5.     Dim x As Integer 'result
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Label3.Text = Label3.Text + 1
  8.  
  9.         Dim selectedanswer As String = ""
  10.  
  11.         If RadioButton1.Checked = True Then selectedanswer = "a"
  12.         If RadioButton2.Checked = True Then selectedanswer = "b"
  13.         If RadioButton3.Checked = True Then selectedanswer = "c"
  14.  
  15.         If selectedanswer = ca(value) Then x = x + 1
  16.         RadioButton1.Checked = False
  17.         RadioButton2.Checked = False
  18.         RadioButton3.Checked = False
  19.         If Label3.Text = 10 Then
  20.             MsgBox("Your Result is " & x)
  21.             Form2.Show()
  22.             me.hide()
  23.  
  24.         End If
  25.  
  26.  
  27.         loadquestions()
  28.  
  29.     End Sub
  30.     Public Sub loadquestions()
  31.  
  32.  
  33.         ' Initialize the random-number generator.
  34.         Randomize()
  35.         ' Generate random value between 1 and 10. 
  36.         value = CInt(Int(((counter - 1) * Rnd()) + 1))
  37.         Label1.Text = q1(value).ToString
  38.         RadioButton1.Text = a1(value).ToString
  39.         RadioButton2.Text = a2(value).ToString
  40.         RadioButton3.Text = a3(value).ToString
  41.  
  42.     End Sub
  43.  
  44.     Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  45.  
  46.     End Sub
  47.  
  48.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  49.         OpenFileDialog1.ShowDialog()
  50.  
  51.  
  52.  
  53.  
  54.         Dim soubor As New IO.StreamReader(OpenFileDialog1.FileName)
  55.         While Not soubor.EndOfStream
  56.             Dim txtline() As String = soubor.ReadLine().Split("|")
  57.             q1(counter) = txtline(0)
  58.             a1(counter) = txtline(1)
  59.             a2(counter) = txtline(2)
  60.             a3(counter) = txtline(3)
  61.             ca(counter) = txtline(4)
  62.             counter = counter + 1
  63.         End While
  64.         loadquestions()
  65.     End Sub
  66. End Class
Apr 15 '15 #1
5 1518
Rabbit
12,516 Expert Mod 8TB
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.
Apr 15 '15 #2
Can u give me an example please?
Apr 17 '15 #3
Rabbit
12,516 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. arrayOfGeneratedNumbers(10)
  2. newNumberGenerated = False
  3.  
  4. Do Until newNumberGenerated = True
  5.    newNumberGenerated = True
  6.    x = random number
  7.  
  8.    For Each y In arrayOfGeneratedNumbers
  9.       If x = y Then
  10.          newNumberGenerated = False
  11.       End If
  12.    Next y
  13. Loop
Apr 17 '15 #4
like this?
declared:
y as int
coutner as string
value as string
Expand|Select|Wrap|Line Numbers
  1. Public Sub loadquestions()
  2.  
  3.         Randomize()
  4.         value = CInt(Int(((counter - 1) * Rnd()) + 1))
  5.  
  6.     arrayOfGeneratedNumbers(10)
  7.     newNumberGenerated = False
  8.  
  9.     Do Until newNumberGenerated = True
  10.       newNumberGenerated = True
  11.        value = random number
  12.  
  13.        For Each y In arrayOfGeneratedNumbers
  14.           If value = y Then
  15.         newNumberGenerated = False
  16.           End If
  17.            Next y
  18.         Loop
  19.  
  20.         Label1.Text = q1(value).ToString
  21.         RadioButton1.Text = a1(value).ToString
  22.         RadioButton2.Text = a2(value).ToString
  23.         RadioButton3.Text = a3(value).ToString
  24.  
  25.  
  26.     End Sub    
  27.  
Apr 20 '15 #5
Rabbit
12,516 Expert Mod 8TB
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.
Apr 20 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
0
by: joaoduraes | last post by:
Hello... I have a Mysql DB with X entries. Each one displays an item to the user. How can i create a script to show all items in a random way (one at a time) without repeating.And once all itens...
1
by: Krimp | last post by:
I pulled this code from Vbasic.net or generating random passwords. I want to know how I can set up so that each time the page is refreshed a new password is generated without having to fill in the...
7
by: Suprim | last post by:
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out? For example, if number...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.