[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

Recursion & Random Numbers

Posted on 2002-06-08
Medium Priority
191 Views
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

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.
0
Question by:balabaster
• 3
• 3
• 2
• +2

LVL 11

Expert Comment

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

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

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.

0

LVL 11

Expert Comment

ID: 7065097
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?
0

LVL 5

Expert Comment

ID: 7065103
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
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
0

LVL 5

Accepted Solution

Julian_K earned 200 total points
ID: 7065105
The idea is that we have to avoid getting a number, which was previously marked as not acceptable by the "Already_Called" function, to minimize the recursion level.
So, when we do recursion, we seek a number, that is higher or lower, but NOT equal to the previous, that was not acceptable. That's it. By the way, when you generate a random number, you use:
a = Int(Rnd * (iHigh - iLow)) + iLow

this returns iLow <= a < (!!!!) iHigh
I mean, you can NEVER get a=iHigh. I'm not sure if this is on purpose, or a bug, but I have coded the previous example to work the same way.
0

LVL 101

Expert Comment

ID: 7065254
learning
0

Author Comment

ID: 7065771
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.

0

LVL 11

Expert Comment

ID: 7066395
0

LVL 5

Expert Comment

ID: 7066638

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
0

Author Comment

ID: 7067662

0

LVL 18

Expert Comment

ID: 7070556
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.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't interesteā¦
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicā¦
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that aā¦
Suggested Courses
Course of the Month20 days, 13 hours left to enroll