-
Nov 9th, 2017, 05:00 PM
#1
Thread Starter
New Member
Visual Basic help with program
I earlier posted about a question for the project, and here I am confused again. I am still just learning Visual Basic, and our teacher barely explained loops and how to use them. This is the program:
Create a Class Marks program that prompts the user for the number of students in the class. When the user clicks OK, the program should randomly generate a 9-digit student number that begins with "071" and marks between 30 and 100, and output the students number, marks and class average in a listbox control.
My code so far:
Code:
Dim total As Integer = Val(TextBox1.Text)
Dim randomNumber As New Random
Dim firstthree As String = "071"
Dim first As Integer = randomNumber.Next(0, 9)
Dim second As Integer = randomNumber.Next(0, 9)
Dim third As Integer = randomNumber.Next(0, 9)
Dim fourth As Integer = randomNumber.Next(0, 9)
Dim fifth As Integer = randomNumber.Next(0, 9)
Dim sixth As Integer = randomNumber.Next(0, 9)
Dim grade As Integer = randomNumber.Next(30, 100)
Dim studentNumber As String = firstthree & "-" & first & second & third & "-" & fourth & fifth & sixth
Dim counter As Integer = 0
If total = 5 Then
Do While counter < 5
Marks.Items.Add(studentNumber & vbTab & grade)
counter += 1
Loop
End If
I'm still working on my code, I'm still learning so my code may not be the greatest. An issue I currently have is when I add the "studentNumber" and "grade" to the listbox they're all the same generated number when it should be all different. I don't know how to add up the grades in the listbox to find the average, so I have no idea on that part. ]If someone could help with that issue and also help with developing the code and program it would be a blessing.
Thanks!
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/pgZlH.png
[2]: https://i.stack.imgur.com/bsmUh.png
Last edited by Shaggy Hiker; Nov 9th, 2017 at 05:06 PM.
Reason: Added CODE tags.
-
Nov 9th, 2017, 05:15 PM
#2
Re: Visual Basic help with program
I edited your post to add [CODE][/CODE] tags, which you can do by pressing the # button and pasting the code between the resulting tags.
One thing you will need to do, eventually, is move the declaration of the Random object outside of the method (this may be the cause of what you are seeing). The reason for this can be obscure, but random number generators all start with a seed. Two generators that start with the same seed will generate identical sequences of numbers. That may not bite you here, but it will eventually, because you seed the random object with the current system time when you create it. If you create more than one a second, due to a loop, or calling a method rapidly, you will end up with numbers that are anything but random. Therefore, the only safe thing to do is to create just ONE random object at form scope, and use it wherever you want.
A second point is that you will eventually want to move away from using Val(). Val is kind of a cool function, but it has some quirks that can surprise people. The one that caught me was Val("1,234"), which is, of course, 1. Val stops at the first character it can't convert, so a comma will stop it. A safer solution would be one of the .TryParse methods, such as Double.TryParse. Better still would be to use a NumericUpDown control for entering numbers rather than a textbox. However, it's good that you are doing a real conversion.
My usual boring signature: Nothing
-
Nov 10th, 2017, 02:47 AM
#3
Re: Visual Basic help with program
You should initialize the random generator, using Randomize() function first.
Randomize Function (Visual Basic)
https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx
-
Nov 10th, 2017, 03:15 AM
#4
Re: Visual Basic help with program
Originally Posted by gibra
No you shouldn't. If you were using Rnd to generate random numbers then that would be sound advice, but you shouldn't be using Rnd. With the Random class, you simply create an instance and you're good to go.
-
Nov 10th, 2017, 12:05 PM
#5
Re: Visual Basic help with program
Randomize should have nothing to do with Random, but I'm not entirely sure that's the case. I've never looked into it, but there have been a few threads on here that made me think that whatever Randomize does, it messes with the Random initialization. It may be superstition, but I would suggest specifically NOT using Randomize if you are using Random. It may not do nothing at all.
My usual boring signature: Nothing
-
Nov 10th, 2017, 02:46 PM
#6
Re: Visual Basic help with program
Originally Posted by Shaggy Hiker
Randomize should have nothing to do with Random, but I'm not entirely sure that's the case. I've never looked into it, but there have been a few threads on here that made me think that whatever Randomize does, it messes with the Random initialization. It may be superstition, but I would suggest specifically NOT using Randomize if you are using Random. It may not do nothing at all.
The trick is there's actually no reliable way to prove yes or no.
If you provide a manual seed to Random, you'll always get the same sequence, so we'd consider it a bug if Randomize() did something to it.
If you try to create two Random() instances:
Code:
Dim rng1 As New Random()
Dim rng2 As New Random()
Well, now if you call Randomize(), if it DID affect them, it would affect both. So you might consider:
Code:
Dim rng1 As New Random()
Randomize()
Dim rng2 As New Random()
But since the parameterless constructor is time-seeded, you can't be 100% certain they received the same seed. In fact, having a Randomize() between them almost guarantees they won't. So the side effect of Randomize() is identical to "maybe some time passed and they got a different seed"!
But there's a good reason not to use Randomize():
Like Val(), MsgBox(), Format(), and all of the other VB built-in methods it says a few things loud and clear. It usually indicates a school/teacher is too lazy/underfunded/stupid/malicious to update their curriculum. Since 1997. For students it's a great "get a refund" warning.
It's not that the functions don't work. But my guess is any curriculum extensively using those functions hasn't updated itself to reflect how professionals write code in 2017. That means its ideas about OOP might be severely out of date. In particular, many exciting new ideas have come forth just since 2010, an era in which VB6 is well past end-of-life. It indicates a really poor quality of class materials to me and I feel sorry for everyone that pays for it.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 11th, 2017, 02:56 AM
#7
Re: Visual Basic help with program
Originally Posted by Shaggy Hiker
Randomize should have nothing to do with Random, but I'm not entirely sure that's the case. I've never looked into it, but there have been a few threads on here that made me think that whatever Randomize does, it messes with the Random initialization. It may be superstition, but I would suggest specifically NOT using Randomize if you are using Random. It may not do nothing at all.
Randomize is actually an archaic remnant of pre-OO versions of Microsoft's Basic products. Like Val, it dates all the way back to QuickBasic. There's no need to be using this in modern versions of VB. From QuickBasic to VB6, random values were generated by the Rnd function and Randomize was the only way to provide a seed.
-
Nov 10th, 2017, 01:21 PM
#8
Re: Visual Basic help with program
An issue I currently have is when I add the "studentNumber" and "grade" to the listbox they're all the same generated number when it should be all different
That's because you only created each one once, and then used a loop to keep adding it over and over to the listbox. If it needs to be different on each row, then you need to do something in the loop that causes the values to change. For example... moving the code to create the student ID and the grade to inside the loop.
-tg
-
Nov 10th, 2017, 06:37 PM
#9
Re: Visual Basic help with program
The Randomize method does pretty much exactly the same thing as the Random constructor. Notice that there are two overloads of Randomize and the documentation says that the parameterless version uses the system time as a seed while the other lets you provide your own seed, just like the Random constructor. Randomize and Rnd together are basically just like one, less convenient instance of the Random class.
-
Nov 10th, 2017, 08:49 PM
#10
Re: Visual Basic help with program
That's what I have always assumed, but I don't know what is happening under the hood. If the random number generator is part of the Random class, then Randomize should have no effect on it. However, if the random number generator is just accessed via the Random class, then Randomize could mess with it.
There would be a way to test this, I just haven't done it. The way to test would look something like this:
Code:
Private myRand As New Random
Private myList as New List(of Integer)
Public Sub Test()
For x=1 to 100000
Randomize()
myList.add(myRand.Next(0,100))
Next
End Sub
If the list contains the same number over and over, then Randomize impacts the Random object, otherwise it doesn't. It seemed silly to propose the test without trying it, so I did, and there appeared no impact of Randomize on Random.
My usual boring signature: Nothing
-
Nov 11th, 2017, 09:13 AM
#11
Re: Visual Basic help with program
I think the consensus is: "Using Randomize() and Rnd() is a practice that manifests as a quirky bug: until you remove it from your code it's very hard for experts to look at the rest of it, so you'd best learn to avoid it."
I really weep for this generation of students. If this many teachers haven't revamped their curriculum since the early 2000s, I can't imagine how bad the quality of the rest of their materials must be. Do they even use/teach OOP? Do we trust at all that they're teaching the ideas that didn't even show up until 2010?
This isn't even a problem "I wish they'd teach C#" would solve. They'd just teach bad C# skills. The only reason it works is the same reason VB6 got such a bad reputation: a poorly-trained programmer does a "good enough" job for about 75% of businesses, and for the rest it takes so long to realize how bad they are it's too late and they're too embarrassed to say the reason they sank.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|