Results 1 to 11 of 11

Thread: Help is Desperately Needed

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    2

    Help is Desperately Needed

    Hi, I am a student at Bournemouth University in my first year of Pyschology and computing. I am really struggling with the computing aprt. We use VB 6.0, and so far this year I have been able to pass off that I know what I am doing as I have had help from lecturers etc, but now our end of year assignment has been set and I don't understand any of it.

    I am pretty much reduced to tears as I don't have a clue.
    I really need some help or advice desperately.

    The assignment is to create a simple bingo program, using for next loops to generate a random card, and a generator to generate random numbers.

    How do I use the for next loop to generate random numbers in random labels? This really confuses me!

    Thanks. Lucy.

  2. #2
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    Welcome to the forums!!

    well its not as hard as u think.. Dont Panic!

    create a form with 1 label in it... call the label something like lblNumber
    now copy and paste it... it will ask you if you want to make a control array.. pick yes
    now keep copying and pasting till you have 24 labels... MAKE SURE you put these
    in order.. meaning copy/paste 1.. then move it into place
    then repeat...
    also.. leave a spot for the middle "free space" you dont want to use one of the labels you just copy and pasted (so it doesnt put a number in it)

    now... use some code like this:
    VB Code:
    1. Private Sub Form_Load()
    2.     'Initialize the Randomizer...
    3.     Randomize
    4.     For x = 0 To lblNumber.UBound 'Loop thru all the labels...
    5.         Select Case x
    6.             Case 0 To 4 'B Column
    7.                 lblNumber(x).Caption = Int(Rnd(1) * 15) + 1
    8.             Case 5 To 9 'I Column
    9.                 lblNumber(x).Caption = Int(Rnd(1) * 15) + 15
    10.             Case 10 To 13 'N Column (Note 1 less since it has the free space)
    11.                 lblNumber(x).Caption = Int(Rnd(1) * 15) + 30
    12.             Case 14 To 18 'G column
    13.                 lblNumber(x).Caption = Int(Rnd(1) * 15) + 45
    14.             Case 19 To 23 'O Column
    15.                 lblNumber(x).Caption = Int(Rnd(1) * 15) + 60
    16.         End Select
    17.     Next
    18.    
    19. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    now.. more details:

    the code above will repeat numbers...
    does that matter? if so.. we can tweak the code to "track" what numbers are used.. (fairly simple)

    if u need more explanation of hats going on.. just let me know!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    2

    Re: Help is Desperately Needed

    Cheers thats brilliant!

    Ill try that now- I don't know about repeating numbers though to be honest! At the moment I dont really care about that! I just wnat this done before I go completely insane!

  5. #5
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    well.. here is version 2:

    it uses only for next and do loops.. no repeats either..

    VB Code:
    1. Private Sub Form_Load()
    2.     'Version 2
    3.     'Using only for next loops...
    4.     Dim Found As Boolean 'To check for duplicates
    5.     Dim tmpNumber As Integer 'Temp storage of random number
    6.     Randomize 'Initialize random
    7.    
    8.     For x = 0 To 4 'loop thru the first 4 labels
    9.         Do
    10.             Found = False '
    11.             tmpNumber = Int(Rnd(1) * 15) + 1 'Pick random number
    12.             For I = 0 To 4 'loop thru the 4 labels
    13.                 If lblNumber(I).Caption = tmpNumber Then Found = True
    14.                 'check to see if another label has the number
    15.                 'if it does, mark Found = true
    16.             Next
    17.         Loop Until Found = False 'If FOUND = True.. do it again
    18.         'If Not.. then set it to the number
    19.         lblNumber(x).Caption = tmpNumber
    20.     Next
    21.     For x = 5 To 9
    22.         Do
    23.             Found = False
    24.             tmpNumber = Int(Rnd(1) * 15) + 15
    25.             For I = 5 To 9
    26.                 If lblNumber(I).Caption = tmpNumber Then Found = True
    27.             Next
    28.         Loop Until Found = False
    29.         lblNumber(x).Caption = tmpNumber
    30.     Next
    31.     For x = 10 To 13
    32.         Do
    33.             Found = False
    34.             tmpNumber = Int(Rnd(1) * 15) + 30
    35.             For I = 10 To 13
    36.                 If lblNumber(I).Caption = tmpNumber Then Found = True
    37.             Next
    38.         Loop Until Found = False
    39.         lblNumber(x).Caption = tmpNumber
    40.     Next
    41.     For x = 14 To 18
    42.         Do
    43.             Found = False
    44.             tmpNumber = Int(Rnd(1) * 15) + 45
    45.             For I = 14 To 18
    46.                 If lblNumber(I).Caption = tmpNumber Then Found = True
    47.             Next
    48.         Loop Until Found = False
    49.         lblNumber(x).Caption = tmpNumber
    50.     Next
    51.     For x = 19 To 23
    52.         Do
    53.             Found = False
    54.             tmpNumber = Int(Rnd(1) * 15) + 60
    55.             For I = 19 To 23
    56.                 If lblNumber(I).Caption = tmpNumber Then Found = True
    57.             Next
    58.         Loop Until Found = False
    59.         lblNumber(x).Caption = tmpNumber
    60.     Next
    61.    
    62. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: Help is Desperately Needed

    Static, I'd change
    VB Code:
    1. Loop Until Found = False
    to
    VB Code:
    1. Loop Until Not Found
    It just creates tighter code.

  7. #7
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    good catch.. guess I rushed
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: Help is Desperately Needed

    I ran it - it barfs on a blank label tested against an integer. I made the following changes:
    VB Code:
    1. [color=red]lblNumber(0).Caption = [color=red]Int[/color]([color=red]Rnd[/color](1) * 15) + 1[/color]
    2.     For x = 1 To 4
    3.         Do
    4.             Found = False
    5.             tmpNumber = Int(Rnd(1) * 15) + 1
    6.             For i = 0 To 4
    7.                 [color=red]If lblNumber(i).Caption > "" Then[/color]
    8.                   If lblNumber(i).Caption = tmpNumber Then Found = True
    9.                 [color=red]End If[/color]
    10.             Next
    11.         Loop Until Not Found
    12.         lblNumber(x).Caption = tmpNumber
    13.     Next x
    Same changes for the other loops (with the appropriate numbers, of course). Nice code, though.

  9. #9
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    Oops.. probably because I had the number 10 in the label already.. design time
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Help is Desperately Needed

    If you use 25 labels, then you don't need to run separate loops:

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim Found As Boolean
    3.     Dim tmpNumber As Integer, X As Integer, I As Integer
    4.     Randomize
    5.    
    6.     For X = 0 To 24
    7.         If Not X = 12 Then
    8.             Do
    9.                 Found = False '
    10.                 tmpNumber = Int(Rnd * 15) + IIf((X \ 5), (X \ 5) * 15, 1)
    11.                 For I = (X \ 5) * 5 To X
    12.                     Found = Found Or (Val(lblNumber(I).Caption) = tmpNumber)
    13.                 Next
    14.             Loop While Found
    15.             lblNumber(X).Caption = tmpNumber
    16.         End If
    17.     Next X
    18. End Sub

  11. #11
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Help is Desperately Needed

    very nice bushmobile... I was thinking something similar but tried to keep it "easy" since
    the poster is having difficulty
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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