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

Hi,

Ive got a bit of a strange one that I cant get my head round. I am trying to write a simple program for doing mathematic flash cards for a friends daughter. Ive 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 its

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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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))
0
Commented:
Maybe I'm not understanding, but if you merely want an int, just cast the result to int, which does a round down.
0
Author Commented:
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.

Lee
0
Director of Information TechnologyCommented:
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)

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
End Function
0
Director of Information TechnologyCommented:
Hmm, PaulHews posted while I was putting mine together.  I like his way better.
0
Commented:
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
0
Commented:
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
0
Commented:
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.
0
Commented:
If (num1/num2)=(num1\num2) then 'This is ok
0
Author Commented:
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
0
Commented:
The code I posted at http:#a20788349 should work... Paste it into a form with a button.
0
Author Commented:
Hi,

Thats seems to work great thank you. Ive 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
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
0
Commented:
You could change it to:
Returned_Number2 = String.Format("{0}", n(1))

The 0 represents the position in the list of parameters you pass to string.format  (Look it up in the help.)

But there are a number of problems with the code.  Unnecessary loops, uncessary variables, etc.

(Note that I didn't fill out all your case statements... Just enough to show you how to do it.)
``````    Private Sub Refresh_Sums2()
Dim i As Integer
Dim a As Integer

Dim TimesText As String

For i = 1 To 12
Dim n() As Integer = GetProblem(Difficulty_Level)

TimesText = n(0).ToString & " " & Chr(247) & " " & n(1).ToString & " ="
'OR use
' TimesText = String.Format("{0} " & Chr(247) & " {1} =", n(0), n(1))
'see how it works?

Select Case i
Case 1
lblTimes1.Text = TimesText
lblsum1.text = n(2).ToString
Case 2
lblTimes2.text = TimesText
lblSum2.text = n(2).ToString
case 3... do the same for the rest.

End Select
Next

End Sub
``````
0

Experts Exchange Solution brought to you by