Welcome to Dream.In.Code
Become a VB Expert!

Join 137,194 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,351 people online right now. Registration is fast and FREE... Join Now!




Having Trouble Generating Non Repeating Random Numbers For A Test Appl

 
Reply to this topicStart new topic

Having Trouble Generating Non Repeating Random Numbers For A Test Appl

DjVw321
25 Jun, 2008 - 06:51 PM
Post #1

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 13


My Contributions
I am making an application for my geography class to help students to study for weekly map tests where it plots a city on a map and the user is to name that city using a list box. I want to make it exactly like the tests that my teacher gives in order to better help students study for them. In our class we are given a list of cities and countries in a region (example: North America) and the list has a total of 67 cities. Out of those 67, the teacher chooses 40 that the students are supposed to answer. No matter what I try I can't seem to get it to choose 40 out of the 67 without repeating the same question. I am using picture boxes for my points on the map so it gets a little lengthy code wise. Basically what happens is the user clicks the next button which assigns a random number to an integer named Questionvalue. Then in a separate sub all picture boxes are set to invisible, or picturebox.visible = false. Then the program uses the value of Questionvalue to turn one of the boxes to visible. Then the user chooses a city from the list box. They click the next button again and the program assigns a value to an integer named answervalue which compares questionvalue and answervalue to see if they match.
I will post the code as a file attachment.


Attached File(s)
Attached File  Code.txt ( 71.83k ) Number of downloads: 16
User is offlineProfile CardPM
+Quote Post

AndyH1963
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
25 Jun, 2008 - 11:54 PM
Post #2

New D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 32


My Contributions
When the code randomly selects a question out of the pool of questions, store the question number in an array and when the code selecst the next question check that the then selected question number is not in the array or previously selected questions. This would not be a very good solution but is a solution.

A better solution would be to have:

(1) Collection of all the possible questions
(2) Collection of remaining questions

The collection in (1) is static and is to ensure that the questions don't get lost. Then when the code selects the random question number of the collection in (2), which initially is the same as the collection in (1) that question is removed from the collection in (2), so that the next selected question will not be the same as that previously selected.

An ideal solution would be to have a single collection of question objects, the object having the following attributes:

integer m_nQuestionID
string m_sQuestion
bool m_bSelected

The m_bSelected attribute is set to false in the constructor, and only when that question is selected is the m_bSelected attribute to to true. The code that selected the questions ignores question object with their m_bSelected attribute which is already set to tru.

HTH
User is offlineProfile CardPM
+Quote Post

DjVw321
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 05:51 AM
Post #3

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 13


My Contributions
I have seen a lot of people doing what you first stated as a possibility by using arrays to check which question was already selected, but I have never tried working with arrays so I was kinda lost when I tried using them to fit my program. And as for what you stated as the best possibility, using the boolean, object and string together, how would I go about doing that. I'm kind of a beginner when it comes to visual basic. Anyway, here is what I have tired to do following your advice.

CODE

Private Sub nextfourtynorepeat()
        Dim qselected As Boolean
        Dim questionvalue As Integer
        Dim q1 As New Object
        Dim q2 As New Object
        Dim q3 As New Object
        Dim q4 As New Object
        Dim q5 As New Object
        Dim q6 As New Object
        Dim q7 As New Object
        Dim q8 As New Object
        Dim q9 As New Object
        Dim q10 As New Object
        Dim q11 As New Object
        Dim q12 As New Object
        Dim q13 As New Object
        Dim q14 As New Object
        Dim q15 As New Object
        Dim q16 As New Object
        Dim q17 As New Object
        Dim q18 As New Object
        Dim q19 As New Object
        Dim q20 As New Object
        Dim q21 As New Object
        Dim q22 As New Object
        Dim q23 As New Object
        Dim q24 As New Object
        Dim q25 As New Object
        Dim q26 As New Object
        Dim q27 As New Object
        Dim q28 As New Object
        Dim q29 As New Object
        Dim q30 As New Object
        Dim q31 As New Object
        Dim q32 As New Object
        Dim q33 As New Object
        Dim q34 As New Object
        Dim q35 As New Object
        Dim q36 As New Object
        Dim q37 As New Object
        Dim q38 As New Object
        Dim q39 As New Object
        Dim q40 As New Object
        Dim q41 As New Object
        Dim q42 As New Object
        Dim q43 As New Object
        Dim q44 As New Object
        Dim q45 As New Object
        Dim q46 As New Object
        Dim q47 As New Object
        Dim q48 As New Object
        Dim q49 As New Object
        Dim q50 As New Object
        Dim q51 As New Object
        Dim q52 As New Object
        Dim q53 As New Object
        Dim q54 As New Object
        Dim q55 As New Object
        Dim q56 As New Object
        Dim q57 As New Object
        Dim q58 As New Object
        Dim q59 As New Object
        Dim q60 As New Object
        Dim q61 As New Object
        Dim q62 As New Object
        Dim q63 As New Object
        Dim q64 As New Object
        Dim q65 As New Object
        Dim q66 As New Object
        Dim q67 As New Object
Start:
        Questionvalue = Int(Rnd() * 67) + 1
        If Questionvalue = 1 Then
            If q1.qselected = True Then
                GoTo start
            End If
            If q1.qselected = False Then
                q1.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 2 Then
            If q2.qselected = True Then
                GoTo start
            End If
            If q2.qselected = False Then
                q2.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 3 Then
            If q3.qselected = True Then
                GoTo start
            End If
            If q3.qselected = False Then
                q3.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 4 Then
            If q4.qselected = True Then
                GoTo start
            End If
            If q4.qselected = False Then
                q4.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 5 Then
            If q5.qselected = True Then
                GoTo start
            End If
            If q5.qselected = False Then
                q5.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 6 Then
            If q6.qselected = True Then
                GoTo start
            End If

            If q6.qselected = False Then
                q6.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 7 Then
            If q7.qselected = True Then
                GoTo start
            End If
            If q7.qselected = False Then
                q7.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 8 Then
            If q8.qselected = True Then
                GoTo start
            End If
            If q8.qselected = False Then
                q8.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 9 Then
            If q9.qselected = True Then
                GoTo start
            End If
            If q9.qselected = False Then
                q9.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 10 Then

            If q10.qselected = True Then
                GoTo Start
            End If
            If q10.qselected = False Then
                q10.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 11 Then
            If q11.qselected = True Then
                GoTo start
            End If
            If q11.qselected = False Then
                q11.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 12 Then
            If q12.qselected = True Then
                GoTo start
            End If
            If q12.qselected = False Then
                q12.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 13 Then
            If q13.qselected = True Then
                GoTo start
            End If
            If q13.qselected = False Then
                q13.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 14 Then
            If q14.qselected = True Then
                GoTo start
            End If
            If q14.qselected = False Then
                q14.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 15 Then
            If q15.qselected = True Then
                GoTo start
            End If
            If q15.qselected = False Then
                q15.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 16 Then
            If q16.qselected = True Then
                GoTo start
            End If
            If q16.qselected = False Then
                q16.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 17 Then
            If q17.qselected = True Then
                GoTo start
            End If
            If q17.qselected = False Then
                q17.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 18 Then
            If q18.qselected = True Then
                GoTo start
            End If
            If q18.qselected = False Then
                q18.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 19 Then
            If q19.qselected = True Then
                GoTo start
            End If
            If q19.qselected = False Then
                q19.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 20 Then
            If q20.qselected = True Then
                GoTo start
            End If
            If q20.qselected = False Then
                q20.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 21 Then
            If q21.qselected = True Then
                GoTo start
            End If
            If q21.qselected = False Then
                q21.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 22 Then
            If q22.qselected = True Then
                GoTo start
            End If
            If q22.qselected = False Then
                q22.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 23 Then
            If q23.qselected = True Then
                GoTo start
            End If
            If q23.qselected = False Then
                q23.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 24 Then
            If q24.qselected = True Then
                GoTo start
            End If
            If q24.qselected = False Then
                q24.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 25 Then
            If q25.qselected = True Then
                GoTo start
            End If
            If q25.qselected = False Then
                q25.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 26 Then
            If q26.qselected = True Then
                GoTo start
            End If
            If q26.qselected = False Then
                q26.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 27 Then
            If q27.qselected = True Then
                GoTo start
            End If
            If q27.qselected = False Then
                q27.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 28 Then
            If q28.qselected = True Then
                GoTo start
            End If
            If q28.qselected = False Then
                q28.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 29 Then
            If q29.qselected = True Then
                GoTo start
            End If
            If q29.qselected = False Then
                q29.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 30 Then
            If q30.qselected = True Then
                GoTo start
            End If
            If q30.qselected = False Then
                q30.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 31 Then
            If q31.qselected = True Then
                GoTo start
            End If
            If q31.qselected = False Then
                q31.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 32 Then
            If q32.qselected = True Then
                GoTo start
            End If
            If q32.qselected = False Then
                q32.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 33 Then
            If q33.qselected = True Then
                GoTo start
            End If
            If q33.qselected = False Then
                q33.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 34 Then
            If q34.qselected = True Then
                GoTo start
            End If
            If q34.qselected = False Then
                q34.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 35 Then
            If q35.qselected = True Then
                GoTo start
            End If
            If q35.qselected = False Then
                q35.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 36 Then
            If q36.qselected = True Then
                GoTo start
            End If
            If q36.qselected = False Then
                q36.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 37 Then
            If q37.qselected = True Then
                GoTo start
            End If
            If q37.qselected = False Then
                q37.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 38 Then
            If q38.qselected = True Then
                GoTo start
            End If
            If q38.qselected = False Then
                q38.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 39 Then
            If q39.qselected = True Then
                GoTo start
            End If
            If q39.qselected = False Then
                q39.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 40 Then
            If q40.qselected = True Then
                GoTo start
            End If
            If q40.qselected = False Then
                q40.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 41 Then
            If q41.qselected = True Then
                GoTo start
            End If
            If q41.qselected = False Then
                q41.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 42 Then
            If q42.qselected = True Then
                GoTo start
            End If
            If q42.qselected = False Then
                q42.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 43 Then
            If q43.qselected = True Then
                GoTo start
            End If
            If q43.qselected = False Then
                q43.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 44 Then
            If q44.qselected = True Then
                GoTo start
            End If
            If q44.qselected = False Then
                q44.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 45 Then
            If q45.qselected = True Then
                GoTo start
            End If
            If q45.qselected = False Then
                q45.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 46 Then
            If q46.qselected = True Then
                GoTo start
            End If
            If q46.qselected = False Then
                q46.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 47 Then
            If q47.qselected = True Then
                GoTo start
            End If
            If q47.qselected = False Then
                q47.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 48 Then
            If q48.qselected = True Then
                GoTo start
            End If
            If q48.qselected = False Then
                q48.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 49 Then
            If q49.qselected = True Then
                GoTo start
            End If
            If q49.qselected = False Then
                q49.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 50 Then
            If q50.qselected = True Then
                GoTo start
            End If
            If q50.qselected = False Then
                q50.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 51 Then
            If q51.qselected = True Then
                GoTo start
            End If
            If q51.qselected = False Then
                q51.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 52 Then
            If q52.qselected = True Then
                GoTo start
            End If
            If q52.qselected = False Then
                q52.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 53 Then
            If q53.qselected = True Then
                GoTo start
            End If
            If q53.qselected = False Then
                q53.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 54 Then
            If q54.qselected = True Then
                GoTo start
            End If
            If q54.qselected = False Then
                q54.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 55 Then
            If q55.qselected = True Then
                GoTo start
            End If
            If q55.qselected = False Then
                q55.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 56 Then
            If q56.qselected = True Then
                GoTo start
            End If
            If q56.qselected = False Then
                q56.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 57 Then
            If q57.qselected = True Then
                GoTo start
            End If
            If q57.qselected = False Then
                q57.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 58 Then
            If q58.qselected = True Then
                GoTo start
            End If
            If q58.qselected = False Then
                q58.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 59 Then
            If q59.qselected = True Then
                GoTo start
            End If
            If q59.qselected = False Then
                q59.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 60 Then
            If q60.qselected = True Then
                GoTo start
            End If
            If q60.qselected = False Then
                q60.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 61 Then
            If q61.qselected = True Then
                GoTo start
            End If
            If q61.qselected = False Then
                q61.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 62 Then
            If q62.qselected = True Then
                GoTo start
            End If
            If q62.qselected = False Then
                q62.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 63 Then
            If q63.qselected = True Then
                GoTo start
            End If
            If q63.qselected = False Then
                q63.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 64 Then
            If q64.qselected = True Then
                GoTo start
            End If
            If q64.qselected = False Then
                q64.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 65 Then
            If q65.qselected = True Then
                GoTo start
            End If
            If q65.qselected = False Then
                q65.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 66 Then
            If q66.qselected = True Then
                GoTo start
            End If
            If q66.qselected = False Then
                q66.qselected = True
                nextnorepeat()
            End If
        End If
        If Questionvalue = 67 Then
            If q67.qselected = True Then
                GoTo start
            End If
            If q67.qselected = False Then
                q67.qselected = True
                nextnorepeat()
            End If
        End If

    End Sub


This post has been edited by DjVw321: 26 Jun, 2008 - 06:06 AM
User is offlineProfile CardPM
+Quote Post

sam_benne
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 06:18 AM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Jan, 2008
Posts: 299



Thanked: 1 times
Dream Kudos: 400
My Contributions
Here go to this link where I have done a snippet
http://www.dreamincode.net/code/snippet1929.htm
User is offlineProfile CardPM
+Quote Post

DjVw321
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 01:04 PM
Post #5

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 13


My Contributions
QUOTE(sam_benne @ 26 Jun, 2008 - 07:18 AM) *

Here go to this link where I have done a snippet
http://www.dreamincode.net/code/snippet1929.htm

I get what that does, but how would I then access those numbers in the list that is shuffled in the order they appear when they are in the list box like i need to do when the person clicks the button for the next question, because as this code is right now it only selects the top of the "deck" and when the button is pressed it shuffles the deck again leaving it open for the person to have the same card twice. Again, I'm an absolute beginner, but I can do a few things with visual basic. I completely understand how you are doing this but I'm just not sure how I could access the numbers to use them in my program.
User is offlineProfile CardPM
+Quote Post

sam_benne
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 01:15 PM
Post #6

D.I.C Regular
Group Icon

Joined: 16 Jan, 2008
Posts: 299



Thanked: 1 times
Dream Kudos: 400
My Contributions
Well what you can do is make those list boxes invisible and create a new listbox. In this one what you do is you say if list2 number 1 = # then it makes a city appear.

Sorry I can't give you the exact code it is because I am on a computer that does not have VB6 but am working on it in notepad.
User is offlineProfile CardPM
+Quote Post

sam_benne
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 01:20 PM
Post #7

D.I.C Regular
Group Icon

Joined: 16 Jan, 2008
Posts: 299



Thanked: 1 times
Dream Kudos: 400
My Contributions
Try something a long the lines of this
CODE

listbox2

if number1 = 1 then
call places
listbox3.additem = place1
end if

...
end sub

function places

1 = "north america"
2 ...
end function

User is offlineProfile CardPM
+Quote Post

DjVw321
RE: Having Trouble Generating Non Repeating Random Numbers For A Test Appl
26 Jun, 2008 - 06:03 PM
Post #8

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 13


My Contributions
Thanks sam_benne for your code snippet. A lot of the stuff in visual basic 2008 is named different than in vb6 so it is kinda confusing starting off with this program because I started off programming using vb6. Anyway, I figured out how to access different array values using the val(myarray(insert variable here)). That was my main problem, I guess.
User is offlineProfile Card