Hi,

I am trying to write a program that
- Generates 3 unique random numbers between 1 and a number entered by the users in an InputBox
- Displays the 3 random number results in labels
- Count the number of Loops
So far I wrote this code but I don't know how to create the randoms to make it work.
Your help is really appreciated.

Private Sub Button1Generate_Click(sender As System.Object, e As System.EventArgs) Handles Button1Generate.Click
        Dim userMsg As String
        userMsg = InputBox("Please Enter A Number?", "Integer", "Enter your integer here", 500, 700)
        Dim GenerateRandom As New Random(DateTime.Now.Millisecond)
        Try
            Dim Random1 As Integer = Integer.Parse(TextBox1Num1.Text)
            Try
                Random1 = GenerateRandom.Next(1, userMsg)

                Do
                    Random2 = GenerateRandom.Next(1, userMsg)

                Loop Until Random1 <> Random2
                Do
                    Random3 = GenerateRandom.Next(1, userMsg)

            Loop Until Rnadom1 <> Random3 And Rnadom2 <> Random 3

                Dim RandomInt As Integer = GenerateRandom.Next(MinInt, MaxInt)
                Label4Result.Text = "Number of Loops = " + RandomInt.ToString

            End Try
    End Sub

I will comment a few points:
1) You need to validate that the userMsg is a valid number between 3 (because is the minimal value acceptable for 3 random integers starting at 1) and the Integer.MaxValue. If it is not, you must launch a message and exit the sub.
2) The second parameter of the function Next of the GenerateRandom random generator, is the max value to generate, so here you must pass the integer value represented in the userMsg.
3) Before starting the generation of random numbers, you need to create a integer loopCounter initialized to 0, to count the loops.
4) Assign the value of 0 to the Random1, Random2 and Random3 before starting the random number generation.
5) To verify the rightness of the returned values, you must also verify that the Random1 is not equals to the Random2.
6) You must assign your returned values to some label to be shown. If you assign an Integer to a Text, VB automatically uses the default method ToString to convert the integer to string.

You can just change the code to some thing like this untested code:

Dim userMsg As String = InputBox("Please Enter A Number?", "Integer", "Enter your integer here", 500, 700)
Dim GenerateRandom As New Random(DateTime.Now.Millisecond)
Dim Random1 as Integer = 0
Dim Random2 as Integer = 0
Dim Random3 as Integer = 0
Dim loopCount as Integer = 0
Try
    Dim m as Integer = CInt(userMsg)
    If m < 3 then
        MsgBox("The value entered must be greather than 2. Try Again.")
        Exit Sub
    End If
    Do
        Random1 = GenerateRandom.Next(1, m)
        Random2 = GenerateRandom.Next(1, m)
        Random3 = GenerateRandom.Next(1, m)
        loopCount += 1
    Loop Until Random1<>Random2 and Random1<>Random3 and Random2<>Random3
    Label1Result.Text = Random1
    Label2Result.Text = Random2
    Label3Result.Text = Random3
    Label4Result.Text = loopCount
Catch ex As Exception
    MsgBox("The value entered is not valid. Try again.")
End Try

Hope this helps

Thank you so much.

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.