Link to home
Start Free TrialLog in
Avatar of balabaster
balabaster

asked on

Recursion & Random Numbers

Okay, here's a fun question for the technically minded.  It's been a while since I've done anything even close to recursion, and I can't find my uni notes.

I've got a Random Number generator which I want to check if numbers have been called already - I guess kind of like a lottery in some ways.

Now I've got my function:

Public Function RandomNum(iLow As Integer, iHigh As Integer) As Integer
Dim a As Integer

    Randomize
    a = Int(Rnd * (iHigh - iLow)) + iLow

    If Not Already_Called(a) Then
        RandomNum = a
    Else
        RandomNum = RandomNum(iLow, iHigh)
    End If

End Function

Now, the calling line is storing the return number to an array which the Already_Called function checks to see if the number has been called previously and returns true if the number is already found in the number array and false if it isn't.  However, upon Already_Called returning True, my RandomNum function generates an "Out of Stack Space" error message when getting nearer the end of the array when only a finite amount of numbers are left.

Can anyone see anything that I have obviously wrong?  I know that a recursive function needs a close or collapse, which I have, if it's not already generated the number, it returns a, otherwise it keeps calling itself until it generates a number it hasn't already called before.

I guess the problem may be caused by the fact that it is feasible that the random number generator could be generating with an iLow of 1 and an iHigh of 10 and upon reaching the last number never calls the only missing number.  Is there an obvious solution for this other than telling it to go and figure out what the last number that hasn't been called is and just calling that (which in my meagre opinion is a bit of a lame workaround).

Thanks for any help anyone can give me.
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Public Function RandomNum(iLow As Integer, iHigh As Integer) As Integer
Dim a As Integer

   Randomize
   a = Int(Rnd * (iHigh - iLow)) + iLow

   If Not Already_Called(a) Then
       RandomNum = a
       Exit Function  'Try adding this line.
   Else
       RandomNum = RandomNum(iLow, iHigh)
   End If

End Function

I am also not very expert in recursion functions, but if you don't mind, try adding that line. I think it should solve your problem. If not, you have to remove just one line to come back to your original code.

On second thoughts, it wouldn't matter actually. Your function looks good to me. Have you thoroughly tested your Already_Called functionality on which this function rests?
Hi.
The error you get means in fact, that the func "Already_Called(a)" returns 'TRUE' too many times, forcing the procedure go get deeper and deeper into a recursion until the stack overflows.

So, you can use a simple cycle instead of recursion. Or, If you insist to use recursion, you have to implement some more code. Might be something like this:

Public Function RandomNum(iLow As Integer, iHigh As Integer) As Integer
   
    Dim blnSeekLow As Boolean
    Dim a As Integer
   
    'closes the recursion,
    'if there are no free numbers in this interval.
    If iLow >= iHigh Then
        RandomNum = -1
        Exit Function
    End If
   
    'try to get a random number
    a = Int(Rnd * (iHigh - iLow)) + iLow

    'check if it is acceptable
    If Not Already_Called(a) Then
        RandomNum = a
        Exit Function
    End If
       
    'if not acceptable,
    'decide where to search for other number - higher or lower than this
    blnSeekLow = (Int(Rnd * 2) = 0)
   
    'get random number, excluding a, in one of the directions
    If blnSeekLow Then
        a = RandomNum(iLow, a)
    Else
        a = RandomNum(a + 1, iHigh)
    End If
   
    'if a>=0 means we have a result
    If a >= 0 Then
        RandomNum = a
        Exit Function
    End If
   
    'If a<0 then there is no result. In that case, we search in the other direction
    blnSeekLow = Not blnSeekLow
    If blnSeekLow Then
        a = RandomNum(iLow, a)
    Else
        a = RandomNum(a + 1, iHigh)
    End If
   
    'if both directions return (a<0) then there is no unused number left.
    If a < 0 Then
        Err.Raise vbObjectError + 1, "Random Number Generator", "There are no free numbers left!"
    Else
        RandomNum = a
    End If

End Function

I think there might be easier way to do it, but that was all I could think out right now.

Hope it is usefull.

Julian
ASKER CERTIFIED SOLUTION
Avatar of Julian_K
Julian_K
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
Mike McCracken

learning
Avatar of balabaster

ASKER

okay, I've got it...I figured it out around the same time as the last post...iHigh was not ever returning the highest number.

I tried calling my function by using:

Const LOW_COUNT = 1
Const HIGH_COUNT = 10

a = RandomNum(LOW_COUNT, HIGH_COUNT + 1)

This is a workaround, but it does fix the problem.  I'll modify the RandomNum function at some point to account for this, but it was running for the last 24 hours without error, so I am going to run with this for now.

Thanks for your help.
Please deal with this thread, if your problem now stands solved.

Well, balabaster, It's up to You, but there is a chance to get this error again, If you use your algorythm: It is possible that the (RND) function to return already-called numbers too many times. Especialy if You use a bigger range (not 1 to 10, but say 1 to 1000 or more).

Anyway, if you write this program just for fun, it is not so important.

Best wishes

Julian
Sorry, guys, I thought I'd already hit "Accept Answer" my apologies.

Consider it Answered
You still need to finalize the question by actually accepting a comment as the answer so that it moves the question to the PAQs and awards the points to the Expert.