Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

how to get 2 random numbers and divide them by each other ensuring the answer is a whole in vb.net

Hi,

Ive got a bit of a strange one that I cant get my head round. I am trying to write a simple program for doing mathematic flash cards for a friends daughter. Ive managed to do the addition, subtraction and multiplication but I am a bit bamboozled on the division. What I am trying to do is get 2 random numbers from 1 to 9999 and divide them by each other. The only problem I have got is that I can not show divisions that the answer is a decimal it has to be a whole number ie: -

3/2 = 1.5

As this is a decimal I do not want to show this sum. If its

6/3 = 2

That is ok as it is a whole number. I am just trying to find a piece of code that is capable of doing this please.

Hope this makes sense.

Many Thanks
Lee
Avatar of PaulHews
PaulHews
Flag of Canada image

This help?

        Dim rng As New Random
        Dim Num1 As Integer = rng.Next(1, 100)
        Dim Num2 As Integer = rng.Next(1, 100)
        Dim Product As Integer = Num1 * Num2

        MsgBox(String.Format("{0}/{1} = {2}", Product, Num1, Num2))
Maybe I'm not understanding, but if you merely want an int, just cast the result to int, which does a round down.
Avatar of ljhodgett
ljhodgett

ASKER

Hi,

Yep the following code is exactually what I'm looking for: -

Dim rng As New Random
        Dim Num1 As Integer = rng.Next(1, 100)
        Dim Num2 As Integer = rng.Next(1, 100)
        Dim Product As Integer = Num1 * Num2

        MsgBox(String.Format("{0}/{1} = {2}", Product, Num1, Num2))

The only thing I have found is that I need to set a so called difficulty level between the following: -

numbers from 1 - 9
numbers from 1 - 99
numbers from 1 - 999
numbers from 1 - 9999

I did try changing the random values but without any joy.

Many thanks for your help.

Lee
Avatar of Joel Coehoorn
Pick the first number at random.  Then get a list of factors for the number, and then pick at random from the list.  This code will create the list, but I would only use it for small n values.  This should do fine for kids' flashcards, but if your n values get large you'll want something more elegant.

Function GetFactors(ByVal n As UInteger) As List(Of Integer)
    GetFactors= New List(Of Integer)
    GetFactors.Add(1)

    For i As Integer = 2 To n /2
        If n Mod i = 0 Then GetFactors.Add(i)
    Next i

    'If n is 1 it will be added twice, but it won't really matter
    GetFactors.Add(n)
End Function
Hmm, PaulHews posted while I was putting mine together.  I like his way better.
Since the 9/9 = 1 types are a little too easy (and occur often) I screen them out.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Difficulty() As Integer = {9, 99, 999, 9999}
        For i As Integer = 0 To Difficulty.GetUpperBound(0)
            Dim n() As Integer = GetProblem(Difficulty(i))
            Debug.WriteLine(String.Format("{0}/{1} = {2}", n(0), n(1), n(2)))
        Next

    End Sub

    'returns product, num1, num2 in array
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()

        Dim Product, Num1, Num2 As Integer
        Dim rng As New Random
        Do

            Num1 = rng.Next(1, DiffMax)
            Num2 = rng.Next(1, DiffMax)
            Product = Num1 * Num2
        Loop Until Product < DiffMax And Num1 <> 1 And Num2 <> 1

        Return New Integer() {Product, Num1, Num2}
    End Function
End Class
Little fix:

    'returns product, num1, num2 in array
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()

        Dim Product, Num1, Num2 As Integer
        Dim rng As New Random
        Do

            Num1 = rng.Next(1, DiffMax + 1)  'last argument is non-inclusive
            Num2 = rng.Next(1, DiffMax + 1)
            Product = Num1 * Num2
        Loop Until Product < DiffMax And Num1 <> 1 And Num2 <> 1

        Return New Integer() {Product, Num1, Num2}
    End Function
Actually, I take it back... Since I'm not using values where Num1 or Num2 = 1, we can't use Num1 or Num2 = DiffMax either.
If (num1/num2)=(num1\num2) then 'This is ok
Hi,

Sorry everyone I'm a bit confused now to which piece of code to use as I'm not overly familiar with functions.

Best Regards
Lee
The code I posted at http:#a20788349 should work... Paste it into a form with a button.
Hi,

Thats seems to work great thank you. Ive made a change to integrate it into the program. Basically it shows 12 maths problems and displays it on the screen including the answers. A so called learning mode for the child. I have tried the following but I get the error: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. On the line: Returned_Number2 = String.Format("{1}", n(1)): -

Private Sub lblDiff1000_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDiff1000.Click
        fraDifficulty.Visible = False
        fraSums.Visible = True
        Difficulty_Level = 9999
        Refresh_Sums2()
    End Sub

    Private Sub Refresh_Sums2()
        Dim i As Integer
        Dim a As Integer
        Dim Returned_Number1 As String = ""
        Dim Returned_Number2 As String = ""

        Dim Difficulty() As Integer = {Difficulty_Level}
        For i = 0 To 24


            For b As Integer = 0 To Difficulty.GetUpperBound(0)
                Dim n() As Integer = GetProblem(Difficulty(b))
                Returned_Number1 = String.Format("{0}", n(0))
                Returned_Number2 = String.Format("{1}", n(1))
            Next


            Select Case i
                Case 1
                    No1 = Returned_Number1
                    No2 = Returned_Number2
                Case 2
                    No3 = Returned_Number1
                    No4 = Returned_Number2
                Case 3
                    No5 = Returned_Number1
                    No6 = Returned_Number2
                Case 4
                    No7 = Returned_Number1
                    No8 = Returned_Number2
                Case 5
                    No9 = Returned_Number1
                    No10 = Returned_Number2
                Case 6
                    No11 = Returned_Number1
                    No12 = Returned_Number2
                Case 7
                    No13 = Returned_Number1
                    No14 = Returned_Number2
                Case 8
                    No15 = Returned_Number1
                    No16 = Returned_Number2
                Case 9
                    No17 = Returned_Number1
                    No18 = Returned_Number2
                Case 10
                    No19 = Returned_Number1
                    No20 = Returned_Number2
                Case 11
                    No21 = Returned_Number1
                    No22 = Returned_Number2
                Case 12
                    No23 = Returned_Number1
                    No24 = Returned_Number2
               
            End Select
        Next

        lblTimes1.Text = No1 & " " & Chr(247) & " " & No2 & " ="
        lblTimes2.Text = No3 & " " & Chr(247) & " " & No4 & " ="
        lblTimes3.Text = No5 & " " & Chr(247) & " " & No6 & " ="
        lblTimes4.Text = No7 & " " & Chr(247) & " " & No8 & " ="
        lblTimes5.Text = No9 & " " & Chr(247) & " " & No10 & " ="
        lblTimes6.Text = No11 & " " & Chr(247) & " " & No12 & " ="
        lblTimes7.Text = No13 & " " & Chr(247) & " " & No14 & " ="
        lblTimes8.Text = No15 & " " & Chr(247) & " " & No16 & " ="
        lblTimes9.Text = No17 & " " & Chr(247) & " " & No18 & " ="
        lblTimes10.Text = No19 & " " & Chr(247) & " " & No20 & " ="
        lblTimes11.Text = No21 & " " & Chr(247) & " " & No22 & " ="
        lblTimes12.Text = No23 & " " & Chr(247) & " " & No24 & " ="


        lblSum1.Text = No1 / No2
        lblSum2.Text = No3 / No4
        lblSum3.Text = No5 / No6
        lblSum4.Text = No7 / No8
        lblSum5.Text = No9 / No10
        lblSum6.Text = No11 / No12
        lblSum7.Text = No13 / No14
        lblSum8.Text = No15 / No16
        lblSum9.Text = No17 / No18
        lblSum10.Text = No19 / No20
        lblSum11.Text = No21 / No22
        lblSum12.Text = No23 / No24


    End Sub

    'returns product, num1, num2 in array
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()

        Dim Product, Num1, Num2 As Integer
        Dim rng As New Random
        Do

            Num1 = rng.Next(1, DiffMax)
            Num2 = rng.Next(1, DiffMax)
            Product = Num1 * Num2
        Loop Until Product < DiffMax And Num1 <> 1 And Num2 <> 1

        Return New Integer() {Product, Num1, Num2}
    End Function


Best Regards
Lee
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial