[RESOLVED] Not allowing new number to be same as last
Hello,
Right now I'm making a program for little kids that will shoot out simple math equations dealing with adding, subtracting, dividing, and multiplying. Everything is working fine except for one small problem on the subtraction part of the program.
I have this function that will create the subtraction problems:
VB Code:
Public Function GetSubtraction(ByVal low As Integer, ByVal high As Integer) As String
intEqCount = intEqCount + 1
[COLOR=Red]Do
MainNum = Rand(low, high)
Loop Until MainNum <> LastMain[/COLOR]
LastMain = MainNum
Do
DoEvents
LowNum = Rand(low, high)
Loop Until LowNum < MainNum
GetSubtraction = MainNum & " - " & LowNum
End Function
the code in red is supposed to make it so that the last first number of the subtraction equation is not the same as the previous one. But it does not work all the time, because sometimes I get repeats. Can someone help me out to not allow the same equation twice in a row?
The sequence does not matter, keep running the program until you get one equation (the equation can be any numbers) say 8-5, and if the program puts 8-5 again, it will think the eight is zero, say the user got the problem wrong, and in the caption below put 0-5 = -5
ok, found the problem, in your Funtion GetSubtraction, sometimes the while loop never ends, you didn't realize about it because you added DoEvents inside that function. It never ends because MainNum sometimes was the minimum possible value, so the second While (looking for a value lower than that, will never find it, then never end). So I removed that ugly DoEvents from there, and created another rand function (1 for MainNum, 1 for LowNum), this way it never selects the lower possible value for MainNum, so that value can be taken by LowNum.
VB Code:
Public Function RandFirst(ByVal low As Long, ByVal high As Long) As Long
Public Function RandSecond(ByVal low As Long, ByVal high As Long) As Long
'This includes the lower possible value
RandSecond = low + Rnd() * (high - low)
End Function
Public Function GetSubtraction(ByVal low As Integer, ByVal high As Integer) As String
intEqCount = intEqCount + 1
Do
MainNum = RandFirst(low, high)
Loop Until MainNum <> LastMain
LastMain = MainNum
Do
LowNum = RandSecond(low, high)
Loop Until LowNum < MainNum
GetSubtraction = MainNum & " - " & LowNum
End Function
If you will use range of numbers not too high (like 0 to 12) you don't need DoEvents, but if it will be higher (i.e. you accept up to 50) then you can add Doevents inside your While loops, but be carefull and never call that functions from inside timer events or things like that.
Also, remember to set intTime = 60 when the time is up, else the next time it starts from 0, generating negative numbers.
The last one, why your Funtions are Public? They are inside a module in your real version? if not, they all should be private.
Great! Thanks jcis, this problem was driving me insane. Since this was an actual program that about 1000 elementary kids are going to use I figured I needed to get that problem fixed.
Sorry I can only give you so many rep points, you deserve more!
Re: [RESOLVED] Not allowing new number to be same as last
No prob , about the rand functions I think this substraction function is the harder you'll get, with sum/multiplication/division I think you don't need 2 different rand functions.