Results 1 to 10 of 10

Thread: Little help with homework please!

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    Little help with homework please!

    Hello everyone,

    I have a little problem that drove me crazy I can't figure it out, I spent hours searching the Web but in vain. I hope that you can help with it. The program must generate 6 unique random numbers but when I click display numbers sometimes it gives me 6 unique numbers and sometimes I get duplicate numbers. I will add the code I have so far and I hope that you can help me out.


    Code:
    Public Class frmMain
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    
            ' close the application
            Me.Close()
        End Sub
    
        Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
    
    
    
            Dim intRandom1 As Integer
            Dim intRandom2 As Integer
            Dim intRandom3 As Integer
            Dim intRandom4 As Integer
            Dim intRandom5 As Integer
            Dim intRandom6 As Integer
            Dim randomGenerator As New Random
    
            ' generate random integers 
    
            intRandom1 = randomGenerator.Next(1, 55)
            intRandom2 = randomGenerator.Next(1, 55)
            intRandom3 = randomGenerator.Next(1, 55)
            intRandom4 = randomGenerator.Next(1, 55)
            intRandom5 = randomGenerator.Next(1, 55)
            intRandom6 = randomGenerator.Next(1, 55)
    
    
            ' display integers
            lblNumbers.Text = Convert.ToString(intRandom1) & "   " & Convert.ToString(intRandom2) & "   " & Convert.ToString(intRandom3) & "   " & Convert.ToString(intRandom4) & "   " & Convert.ToString(intRandom5) & "   " & Convert.ToString(intRandom6)
    
            
        End Sub
    End Class

    Thank you so much

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Little help with homework please!

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim r As New Random
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim selectedNumbers As New List(Of Integer)
    7.         For x As Integer = 1 To 6
    8.             Dim number As Integer = r.Next(1, 55) 'pick a number between 1 & 54
    9.             If Not selectedNumbers.Contains(number) Then
    10.                 selectedNumbers.Add(number)
    11.             Else
    12.                 x -= 1
    13.             End If
    14.         Next
    15.         lblNumbers.Text = String.Join(" ", Array.ConvertAll(selectedNumbers.ToArray, Function(x) x.ToString))
    16.     End Sub
    17.  
    18. End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Little help with homework please!

    <sarcastic>
    I have to agree with Paul that you are definitely not smart enough to write the code for yourself, even with our help, and you should just copy and paste someone else's code. There's no need to bother about understanding it and I doubt that your teacher or classmates would view it as cheating.
    </sarcastic>
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Little help with homework please!

    Quote Originally Posted by jmcilhinney View Post
    <sarcastic>
    I have to agree with Paul that you are definitely not smart enough to write the code for yourself, even with our help, and you should just copy and paste someone else's code. There's no need to bother about understanding it and I doubt that your teacher or classmates would view it as cheating.
    </sarcastic>
    it'd take me twice as long to explain the steps to take + make myself understood as it would to post code.

    anyway. i'd learn from the code myself...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Little help with homework please!

    Quote Originally Posted by .paul. View Post
    it'd take me twice as long to explain the steps to take + make myself understood as it would to post code.
    Then take twice as long.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Little help with homework please!

    i don't just post unintelligible code with no explanation. explanations are available on request.

    <sarcastic>
    i could just tell members to read up on specific methods on MSDN. but why bother having a forum in that case? we could just have a page that redirects you to MSDN.</sarcastic>

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Little help with homework please!

    Quote Originally Posted by .paul. View Post
    <sarcastic>
    i could just tell members to read up on specific methods on MSDN. but why bother having a forum in that case? we could just have a page that redirects you to MSDN.</sarcastic>
    Forums should be for when you have tried to find the information you need but can't or have found the information you need but can't understand it. If someone knows what type they need to use or even what member of that type and they don't read the documentation for that type or member then they haven't really tried. When someone doesn't know what type or member to use then posting on a forum is a good option. We can then let them know what type or member to use and they can then research how to use it.

    I'm not criticising the OP here, because they have obviously made an effort and they said that it's homework. It would not have been difficult to explain the principles required to make the code do as desired. The OP can then improve themselves by using their own brain to apply those principles and, if they can't, so be it. There's no rule that says that everyone has to get full marks for every assignment. If you have the information at your disposal and you can't make use of it then you don't deserve full marks.

    It really comes down to two things:

    1. Cheating is wrong.
    2. Posting code to copy and paste is not the only, or even necessarily the best, way to help.

    I will say no more on this off-topic matter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Little help with homework please!

    Quote Originally Posted by jmcilhinney View Post
    Forums should be for when you have tried to find the information you need but can't or have found the information you need but can't understand it. If someone knows what type they need to use or even what member of that type and they don't read the documentation for that type or member then they haven't really tried. When someone doesn't know what type or member to use then posting on a forum is a good option. We can then let them know what type or member to use and they can then research how to use it.

    I'm not criticising the OP here, because they have obviously made an effort and they said that it's homework. It would not have been difficult to explain the principles required to make the code do as desired. The OP can then improve themselves by using their own brain to apply those principles and, if they can't, so be it. There's no rule that says that everyone has to get full marks for every assignment. If you have the information at your disposal and you can't make use of it then you don't deserve full marks.

    It really comes down to two things:

    1. Cheating is wrong.
    2. Posting code to copy and paste is not the only, or even necessarily the best, way to help.

    I will say no more on this off-topic matter.
    <sarcastic>Did you give .paul. bad reputation too because of his "cheating"?</sarcastic>

    ... i am with .paul. on this and I believe that if people are interested in learning they will look at my code and see the methodology behind it

    BTW jmcilhinney I also think that if people give bad rep to others who themselves believe that they are helping, it actually discourages people from being helpful in the first place, PM'ing would be a better solution than to give someone negative Rep.

    Kris

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Little help with homework please!

    Quote Originally Posted by i00 View Post
    <sarcastic>Did you give .paul. bad reputation too because of his "cheating"?</sarcastic>
    I tried, but I was unable to because I had too recently given positive rep for another post.
    Quote Originally Posted by i00 View Post
    ... i am with .paul. on this and I believe that if people are interested in learning they will look at my code and see the methodology behind it
    You're right about that in many cases but what about those who aren't interested in learning? They'll copy and paste your code and get full marks for their assignment when those who actually try for themselves may not. Congratulations on cheating.
    Quote Originally Posted by i00 View Post
    BTW jmcilhinney I also think that if people give bad rep to others who themselves believe that they are helping, it actually discourages people from being helpful in the first place, PM'ing would be a better solution than to give someone negative Rep.
    I disagree. I sent Paul a PM some time ago on this topic. Seems to have done exactly zero. Negative rep sure got your attention. I've received negative rep myself in the past when people have disapproved of my post.

    Anyway, we're well off-topic, for which I take full responsibility.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Little help with homework please!

    Rather than engage in that bickering, let's just take a look at the code.
    Code:
    Dim selectedNumbers As New List(Of Integer)
    This is one approach, and a good one, however, for a student fairly recent to coding, introducing a List (of T) might be considerably advanced. An array of Integer will work just as well for this since you know that you only need 6 numbers. You certainly need a collection of some sort, whether a list or an array, since you need to be able to remember the numbers that you had already selected.


    The following loop works best for a list. If you use an array, you can do the same thing, but you would need to loop from 0 to 5 rather than 1 to 6, since arrays (like all collections) are 0 based. The problem is that you may have to repeat this loop more than just 6 times. .Paul. is decrementing the iterator (x) if a matching number is drawn. That's one way to do it, but if I was the teacher, I'd be suggesting that a better solution would be to get rid of the For loop in favor of a Do loop. You might want to implement the loop in that fashion. Messing with the iterator in a for loop is probably not a good habit to get into.
    Code:
            For x As Integer = 1 To 6
                Dim number As Integer = r.Next(1, 55) 'pick a number between 1 & 54
                If Not selectedNumbers.Contains(number) Then
                    selectedNumbers.Add(number)
                Else
                    x -= 1
                End If
            Next
    The list or array is just holding the values that you have already seen. This line:
    Code:
    If Not selectedNumbers.Contains(number) Then
                    selectedNumbers.Add(number)
    is checking that the collection contains the number already. If it doesn't, then it is added. If you aren't familiar with the .Contains method, then you might write this using a loop to check the elements in the collection. Frankly, checking that the element is found in the collection is going to take more code and will be harder for a different person to understand, which makes it harder to maintain. However, it also might be faster. Therefore, if you aren't familiar with .Contains, try replacing that line with a loop that does the same thing.

    Code:
            lblNumbers.Text = String.Join(" ", Array.ConvertAll(selectedNumbers.ToArray, Function(x) x.ToString))
    This is the one line with which I thoroughly disagree. If you use this, you might as well stamp "SOMEBODY ELSE WROTE THIS" across the top. Nobody who is given the problem of coming up with six unique random numbers, is going to know about the items being used in that line. Lambdas? Array.ConvertAll? Nice to know, but certainly not introductory material. Fortunately, you already know how to write this line, and have done so in a format that would be more reasonable.

    .Paul. gave you a solution. I would suggest that you re-write that solution using just the elements you know. If you don't know List, then work with arrays. Exchange the For loop for a Do loop. Replace that final statement with the one you already showed us. Learn from the example, but don't use it.
    My usual boring signature: Nothing

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