|
-
Oct 6th, 2011, 10:53 PM
#1
Thread Starter
New Member
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
-
Oct 6th, 2011, 11:06 PM
#2
Re: Little help with homework please!
try this:
vb Code:
Public Class Form1
Dim r As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selectedNumbers As New List(Of Integer)
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
lblNumbers.Text = String.Join(" ", Array.ConvertAll(selectedNumbers.ToArray, Function(x) x.ToString))
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 6th, 2011, 11:17 PM
#3
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>
-
Oct 6th, 2011, 11:21 PM
#4
Re: Little help with homework please!
 Originally Posted by jmcilhinney
<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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 6th, 2011, 11:36 PM
#5
Re: Little help with homework please!
 Originally Posted by .paul.
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.
-
Oct 6th, 2011, 11:45 PM
#6
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>
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 7th, 2011, 12:35 AM
#7
Re: Little help with homework please!
 Originally Posted by .paul.
<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.
-
Oct 7th, 2011, 08:29 AM
#8
Re: Little help with homework please!
 Originally Posted by jmcilhinney
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
Last edited by i00; Oct 7th, 2011 at 08:33 AM.
-
Oct 7th, 2011, 09:09 AM
#9
Re: Little help with homework please!
 Originally Posted by i00
<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.
 Originally Posted by i00
... 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.
 Originally Posted by i00
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.
-
Oct 7th, 2011, 09:33 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|