Simple math problem! Newbee
Hello guys, I hope you can help me with this little problem.
Basically, I want to divide two numbers together and they should be 2 random numbers from 1-12 (I have done that part) but, the answer should be a whole number (i.e. Integer, so no decimal points etc)
So, I did the following:
' Initialize the random-number generator.
Randomize()
' Generate random value between 1 and 12.
Dim value5 As Integer = CInt(Int((12 * Rnd()) + 1))
Label3.Text = value5
' Generate random value between 1 and 12.
Dim value6 As Integer = CInt(Int((12 * Rnd()) + 1))
Label4.Text = value6
'make sure its only whole numbers when dividing
If Int(value5 / value6) = False Then
Do Until Int(value5 / value6) = True
Loop
but the problem here is that the program crashes after only a few clicks on the button.
So I guess the question is: How can I make the program generate two numbers (from 1-12) that when divided become a whole number?
(Is there any code that may tell the random generator that I want the numbers to be even?-so all even numbers from 1-12?)
Thanks!
Re: Simple math problem! Newbee
If you only want an even number between 1 and 12 then just generate a random number between 1 and 6 and double it!
You are also using a very oldfashioned method of generating random numbers - you should really be using the System.Random class.
Re: Simple math problem! Newbee
I suggest you really think this through a second time.
You seem to be trying to do this along the lines of "keep dividing two random numbers until you happen to find two that divide to an integer". That's not a very good solution, is it?
It would be better if your second random number would be based on your first. For example, suppose the first random number comes up as 5.
Now, call your second random number x. What should x be such that 5/x is an integer?
It can only be 1 (5/1 = 5) or 5 (5/5 = 1). Anything else results in a fraction.
There is a simple logic: a number x divided by a number y is only integer if x is a multiple of y (including x = y).
So your code should reflect this: once the first random number is generated, the second random number should not be any number between 1 and 12; rather, it should be a random number between 1 and 12 such that the first number is a multiple of the second number (or the same number).
I'm sure you can solve this problem by hand, on paper. Try it with a few first random numbers (8, 12, 6, 1, for example). Try to figure out which numbers between 1 and 12 divide those numbers into an integer, and you will understand how to generate those numbers.
Once you know which numbers are possibly candidates for random number 2, you still have to select one of them randomly. An easy way to do that is to put them in an array, generate a random index within the bounds of that array, and extract that element:
vb.net Code:
Dim rnd As New Random
Dim numbers() As Integer = New Integer() {3, 6, 8, 12}
'Generate a random index within the array's bounds
Dim randomIndex As Integer = rnd.Next(0, numbers.Length)
'and use that to extract the corresponding number from the array
Dim yourNumber As Integer = numbers(randomIndex)