Results 1 to 14 of 14

Thread: [RESOLVED] Not allowing new number to be same as last

  1. #1

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Resolved [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:
    1. Public Function GetSubtraction(ByVal low As Integer, ByVal high As Integer) As String
    2.     intEqCount = intEqCount + 1
    3.     [COLOR=Red]Do
    4.         MainNum = Rand(low, high)
    5.     Loop Until MainNum <> LastMain[/COLOR]
    6.     LastMain = MainNum
    7.     Do
    8.         DoEvents
    9.         LowNum = Rand(low, high)
    10.     Loop Until LowNum < MainNum
    11.     GetSubtraction = MainNum & " - " & LowNum
    12. 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

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Private Function rand(low As Integer, high As Integer) As Integer
    2.     rand = low + Rnd() * (high - low)
    3. End Function
    Also don't forget to Randomize at the beginning

  3. #3

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Not allowing new number to be same as last

    did you just change the function for the random numbers?

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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

  5. #5

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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.
    Attached Files Attached Files

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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

  7. #7

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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
    Attached Files Attached Files

  8. #8

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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:
    1. With lstSelectOp
    2.         .AddItem ("Addition")
    3.         .AddItem ("Subtraction")
    4.         .AddItem ("Multiplication")
    5.         .AddItem ("Division")
    6.         .AddItem ("Random")
    7.     End With

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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..

  10. #10

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. Public Function RandFirst(ByVal low As Long, ByVal high As Long) As Long
    2.     'This will never be the lower possible value
    3.     RandFirst = (low + 1) + Rnd() * ((high - low) - 1)
    4. End Function
    5.  
    6. Public Function RandSecond(ByVal low As Long, ByVal high As Long) As Long
    7.     'This includes the lower possible value
    8.     RandSecond = low + Rnd() * (high - low)
    9. End Function
    10.  
    11. Public Function GetSubtraction(ByVal low As Integer, ByVal high As Integer) As String
    12.     intEqCount = intEqCount + 1
    13.     Do
    14.         MainNum = RandFirst(low, high)
    15.     Loop Until MainNum <> LastMain
    16.     LastMain = MainNum
    17.     Do
    18.         LowNum = RandSecond(low, high)
    19.     Loop Until LowNum < MainNum
    20.     GetSubtraction = MainNum & " - " & LowNum
    21. 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.
    Last edited by jcis; Mar 29th, 2006 at 03:51 PM.

  12. #12

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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!

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  14. #14

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width