Visual Basic Random Number/Mod

685 views Asked by At

I am currently working on some homework for my Visual Basics class

I have to generate two random numbers, then either add them, subtract them, divide them, or multiply them. (the user would try to guess the answer)

The only problem I am going to run into is when I want to divide.

I need the numbers to be generated, and be divisible. I am so lost of how I could do that, and google, and reading through my textbook is no help.

1

There are 1 answers

0
David Wilson On

This function will generate a Tuple of two numbers which when the first is divided by the second, you'll get an answer that is an integer. A Tuple is simply collection of related pieces of data, kind of like a Structure

Private Function GenerateNumbers() As Tuple(Of Integer, Integer)
    Dim rnd As New Random
    'The two lines below generate a
    Dim x As Integer = rnd.Next(99) + 1
    Dim y As Integer = rnd.Next(99) + 1
    Do Until x Mod y = 0
        x = rnd.Next(99) + 1
        y = rnd.Next(99) + 1
    Loop
    Dim result As New Tuple(Of Integer, Integer)(x, y)
    Return result
End Function

To access the numbers, use something like this in your code ..

Dim number1, number2 As Integer
Dim NewNumbers As Tuple(Of Integer, Integer)
NewNumbers = GenerateNumbers()
number1 = NewNumbers.Item1
number2 = NewNumbers.Item2