[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?
thanks
Re: Not allowing new number to be same as last
It seems the problem is in your rand() function, with this one, it works fine..
VB Code:
Private Function rand(low As Integer, high As Integer) As Integer
rand = low + Rnd() * (high - low)
End Function
Also don't forget to Randomize at the beginning
Re: Not allowing new number to be same as last
did you just change the function for the random numbers?
Re: Not allowing new number to be same as last
Quote:
Originally Posted by paralinx
did you just change the function for the random numbers?
Sorry I edited my post, your code is ok, I just created a new rand() function that works, i can't see yours because you didn't post it
1 Attachment(s)
Re: Not allowing new number to be same as last
It still doesn't work, let me load up my form so you can see my problem.
Re: Not allowing new number to be same as last
Don't upload just Forms, things fire errors everywhere, you have to upload its project also
1 Attachment(s)
Re: Not allowing new number to be same as last
I usually zip up the form and project, then send it. But I'm on the limited school computer right now.
When I tried to upload the project into vbf, it says Invalid file for .vbp :confused:
Re: Not allowing new number to be same as last
I saved the form that I uploaded in the above post and it should work fine if you put this into the form load event
VB Code:
With lstSelectOp
.AddItem ("Addition")
.AddItem ("Subtraction")
.AddItem ("Multiplication")
.AddItem ("Division")
.AddItem ("Random")
End With
Re: Not allowing new number to be same as last
Could you give an example of the problem?
8-2, 6-3, 9-1, 1-0 (this is all ok)?
Give an example of a wrong secuence..
Re: Not allowing new number to be same as last
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
Re: Not allowing new number to be same as last
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
'This will never be the lower possible value
RandFirst = (low + 1) + Rnd() * ((high - low) - 1)
End Function
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.
Re: Not allowing new number to be same as last
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.
Re: [RESOLVED] Not allowing new number to be same as last
No, I only used the two random functions for just the subtraction.
thanks again. :thumb: