I am currently taking a programming class. I finished my work early one day and was bored so.. I decided to make a randomizer that will only pick a number a single time. I have been trying to make it, but I run into continous errors with it. It seems to work until the randomizer picks 1. I have no idea why this does not work, but if it does pick one several clicks later the program will crash. Also the program can start picking the other numbers multiple times. Eventually the option for one will go away, but only after several clicks of the button.
Button1 adds the numbers to a text file and button 2 picks the number. Button 3 is a reset.
This program was created to pick seats so I am using that as my variable.
Here is my code so far....

Public Class Form1
    Public seat As Integer
    Dim r As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.Computer.FileSystem.FileExists("seats.txt") Then
            My.Computer.FileSystem.DeleteFile("seats.txt")
            For x = 1 To 5
                My.Computer.FileSystem.WriteAllText("seats.txt", Environment.NewLine + x.ToString, True)
            Next
            RemoveAtLine("seats.txt", 0)
        Else : For x = 1 To 5
                My.Computer.FileSystem.WriteAllText("seats.txt", Environment.NewLine + x.ToString, True)
            Next
            RemoveAtLine("seats.txt", 0)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        r = IO.File.ReadAllLines("seats.txt").Length + 1
        Dim reader As New System.IO.StreamReader("seats.txt")
        Dim allLines As List(Of String) = New List(Of String)
        Do While Not reader.EndOfStream
            allLines.Add(reader.ReadLine())
        Loop
        reader.Close()
        If Val(TextBox1.Text) <= r Then
            For x = 1 To Val(TextBox1.Text)
                Randomize()
                seat = Int(Rnd(1) * r)
                Label1.Text = Label1.Text + Environment.NewLine + ReadLine(seat, allLines)
                RemoveAtLine("seats.txt", seat - 1)
            Next x
        Else : MsgBox("Not enough seats left")
        End If
    End Sub

    Public Shared Sub RemoveAtLine(ByVal filePath As String, Optional ByVal lineRemove As Integer = -1)
        If (String.IsNullOrEmpty("seats.txt")) Then
            Return
        End If
        Dim lines As New List(Of String)(IO.File.ReadAllLines("seats.txt"))
        If (lineRemove >= 0 And lineRemove <= lines.Count) Then
            lines.RemoveAt(lineRemove)
        End If
        IO.File.WriteAllLines("seats.txt", lines.ToArray())
    End Sub

    Public Function ReadLine(ByVal lineNumber As Integer, ByVal lines As List(Of String)) As String
        If lineNumber = 0 Then
            Return lines(lineNumber)
        Else
            Return lines(lineNumber - 1)
        End If
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If My.Computer.FileSystem.FileExists("seats.txt") Then My.Computer.FileSystem.DeleteFile("seats.txt")
    End Sub
End Class

Try using the Random class as in

Dim rand As New Random
Randomize()
Me.Text = rand.Next(1, 11)

rand.Next(min,SortOfMax)

returns a random number in the range min to SortOfMax-1

so rand.Next(1,11) returns a random integer from 1 to 10. I use the name SortOfMax because the Microsoft documentation used to say Next(min,max) which was highly misleading. Now it says

Next(Int32, Int32) Returns a random number within a specified range.

which is still misleading.

This is so much easier than what I was doing. I feel like I have wasted 20 minutes of my life creating the code above.
Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.