Solved

Recursion & Random Numbers

Posted on 2002-06-08
10
149 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:balabaster
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 7065096
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.

0
 
LVL 6

Expert Comment

by:Nitin Sontakke
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

by:Julian_K
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
    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
0
 
LVL 5

Accepted Solution

by:
Julian_K earned 50 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 100

Expert Comment

by:mlmcc
ID: 7065254
learning
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:balabaster
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.

Thanks for your help.
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 7066395
Please deal with this thread, if your problem now stands solved.
0
 
LVL 5

Expert Comment

by:Julian_K
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

by:balabaster
ID: 7067662
Sorry, guys, I thought I'd already hit "Accept Answer" my apologies.

Consider it Answered
0
 
LVL 18

Expert Comment

by:mdougan
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now