out of stack space error - help!!

# Thread: out of stack space error - help!!

1. Senior Member
Join Date
Dec 1969
Posts
127

## out of stack space error - help!!

I have a recursive sub routine that calls itself until a needed number is randomly generated at the very beginning of the sub.<BR><BR>What is strange is that not all the time does the "Out of stack space" error happens, about 50% and the times the Sub works it generates different results.<BR><BR>If I take out randomize() then, it works ok, but I only get 1 set of results obviously, but i need different ones as well.<BR><BR>Below is pseudo code:<BR><BR>Sub Check(param1,param2,param3)<BR>randomize()<BR>num= Int(param3 * rnd())<BR><BR>if num is checked for condition 1 then<BR>call Check(param1,param2,param3)<BR>elseif num is checked for condition 2 then<BR>call Check(param1,param2,param3)<BR>elseif num is checked for condition 3 then<BR>call Check(param1,param2,param3)<BR>else<BR>do stuff<BR>end if<BR>End sub<BR><BR>Please, let me know if you I&#039;m too vague about this and I&#039;ll try to elaborate more.

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: out of stack space error - help!!

Rnd returns a number in range 0.0 to 1.0 which might result in you checking going askew<BR><BR>What are your 3 conditions?<BR><BR>If they are an actual number then you could be calling the sub many many times!

3. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Randomize should only..

.. need to be called once. No?<BR><BR>So, do that before you start calling the recursive routine.

4. Senior Member
Join Date
Dec 1969
Posts
127

## RE: out of stack space error - help!!

param3 is constant as long as the routine runs. It is basically upper range for random numbers. In this case i&#039;m using 6.<BR><BR>param 1 is a location in one dimension of an array<BR>param 2 is a location in another dimension of an array.<BR><BR>I need to sort the numbers in that array a certain way and if my conditions don&#039;t meet I need to get a different number and check it for the conditions again.<BR><BR>Below is pseudo code: <BR><BR>Sub Check(param1,param2,param3) <BR>randomize() <BR>num=Int(param3 * rnd()) <BR><BR>if num is checked for condition 1 then <BR>call Check(param1,param2,param3) <BR>elseif num is checked for condition 2 then <BR>call Check(param1,param2,param3) <BR>elseif num is checked for condition 3 then <BR>call Check(param1,param2,param3) <BR>else <BR>do stuff <BR>end if <BR>End sub <BR>

5. Senior Member
Join Date
Dec 1969
Posts
127

## RE: Randomize should only..

i&#039;ve tried it a numerous ways.<BR><BR>Not calling randomize() at all, give same result every time I reload a page.<BR><BR>Calling randomize() in the beginning of a page or inside of the sub routine again gives about 50% success rate and 50% "out of stack space" error<BR><BR>is there anyway to catch the error and reload the page or routine?

6. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Ok - you are being forced..

.. in the function a HUGE amount of times.<BR><BR>You need to rewrite the code so that it doesn&#039;t recurse excessively.

7. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: out of stack space error - help!!

param3 is a fixed number FTSOA 3<BR><BR>So your range of numbers is ALways going to be in the range<BR>&#062;= (3 * 0.0) to &#060; (3 * 1.0) So what are the conditions?<BR><BR>Would it not be easier to ensure that the result of the above always meets the Three (as yet unknown conditions?) Are these conditions a state secret? Maybe God or I could help with the conditioning?

8. Senior Member
Join Date
Dec 1969
Posts
127

## RE: out of stack space error - help!!

range is between 0 and 6, I Int() result of rnd() function.<BR><BR>i have an array, arr(n,t) and I need to fill it with numbers. For every slot I call this Check function to get a random number and if it meets the condtions, put it into arr. <BR><BR>so i do 2 loops 0 to n and inside of that 0 to t. For each 0 to t slot I call check().<BR><BR>In check() i generate a random number and check:<BR><BR>if a slot is already filled<BR> call check()<BR>elseif (generated number can&#039;t equal the number of the loop, if i&#039;m on loop 3 inside 0 to t then number can&#039;t be 3)<BR> call check()<BR>elseif (generated number can&#039;t equal any number that is already in any of arr(n*,currentt)<BR> call check()<BR>else<BR> fill the slot with the number at arr(currentn,currentt)<BR> also fill a slot at arr(currentn,generated number)=currentt<BR>end if<BR>end sub

9. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Could you do this with a...

.. (another) loop instead of the recursive function?

10. Senior Member
Join Date
Dec 1969
Posts
127

## RE: Could you do this with a...

it all comes down to needing that new random number and trying it again to see if it fits, that&#039;s why i use recursive function.<BR>

#### Posting Permissions

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