Reputation: 1838
i am trying to programmatically find value of x and y in equation 146 + x + y divided by 7 = 28.13% x and y contains any value between 19 and 43. here is the code i am getting 25 for both x and y ,what am i doing wrong?
Public Class Form1
Dim percentage, x, y, f As Integer
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Dim Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
percentage = 28.13
Do Until f = 4
If (146 + x + y) / 7 = percentage Then
MessageBox.Show(x)
MessageBox.Show(y)
f = 4
End If
x = GetRandom(19, 43)
y = GetRandom(19, 43)
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = 19
y = 19
End Sub
End Class
Upvotes: 0
Views: 1527
Reputation: 25013
You are looking for (x+y)=50.91 where x is in [19, 43] and y is in [19, 43].
Therefore, choose an x, and then y=50.91-x
There is no single solution to the equation you have shown.
Or are you actually asking for something else?
Upvotes: 2
Reputation: 81610
1) You declared percentage
as an integer but are trying to set it to 28.13, which is not an integer.
2) Don't keep recreating a new random object:
Public Class Form1
Private Generator As New Random()
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Return Generator.Next(Min, Max)
End Function
3) If you have to equal 28.13, then that probably won't work without declaring percentage as a decimal and then using Math.Round(...) on your equation, but even then, I don't think you will have any x and y integers that will satisfy the equation for precise equality.
Upvotes: 2