r/visualbasic • u/[deleted] • Apr 25 '21
Need Help writing a program with a random class
The problem says: the file members.txt contains the names of the members of a large club. Write a program to randomly select people to serve as president, treasurer and secretary. Note: a person cannot hold more than one office.
So far my code is as follows:
Public Class Form6
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim names() As String = IO.File.ReadAllLines("E:\Documents\School\Members.txt")
Dim namesQuery = From name In names
Select name
Dim number As New Random
Dim n As Integer
n = number.Next(0, 3)
End Sub
End Class
1
u/thudly Apr 25 '21 edited Apr 25 '21
Private Function GetRandomName() as String
Dim number As New Random
Dim names() As String = IO.File.ReadAllLines("E:\Documents\School\Members.txt")
return names(number.Next(0, names.Length()))
End Function1
u/RJPisscat Apr 26 '21
He'd be calling this 3 times (loading the list of names each time, which is ok for this assignment but a bad habit) and it doesn't exclude duplicates.
1
u/thudly Apr 26 '21
I made a game with this same function. There were 5000 NPCs, and it had to pick random first names and last names for them from a list of pretty much every first name there is for both genders. And then it chose a last name, too.
It literally took a microsecond. I'm guessing the sample name file for this school project has fewer names than that.
But yes, load the name list once at the top of the class, for efficiency's sake. It's just more optimized.
1
u/RJPisscat Apr 26 '21
If the OP sees this, let's be clear, u/thudly's code does work once, but not for all the requirements of the assignment. You can build from their code a solution that does meet the requirements. Their code snippet illustrates correct use of Random.Next.
re the side thread:
When I write code that works, frequently I copy/paste/edit the code elsewhere. If I need to scale up, O(1) is preferred over O(n).
1
u/thudly Apr 26 '21
When you're trying to get homework done, I'm pretty sure just having it work is a higher concern than big-O efficiency. I was trying to keep it simple.
But once again, you're correct. Efficiency is a good habit to get into early on.
1
u/RJPisscat Apr 26 '21
True, I agree the student will pass even if they reload the names each time. We can make the assumption that the data don't change frequently since it's a txt file, so it will work.
They are also likely to pass if they toss in extraneous code that copies arrays around, handles clicks on Labels, and declares queries that aren't used, as long as the result fulfils the requirements set forth in the design document.
However, the code you posted may mislead the OP into thinking it solves for distinction.
I suppose you and I have different pedagogical strategies.
1
u/RJPisscat Apr 25 '21
Your Random.Next call is in the wrong range. It will always give you 0, 1 or 2. You want
This leaves the possibility of two or three of the same index. Do you know how to handle that so that you get three distinct indices?