quote:
Originally posted by stu
foaly, I just finished trying your code, it did seem to be working at first, but then it gave me an error in the script debuggercode:
Error: Out of stack space (code: -2146828260)
File: test.js. Line: 68.
Foaly used a
recursive function.
Recursive functions are functions which will call themselfs. eg:
code:
// when you call the next funcion with its parameter set to True, it will constantly call itself and never end.
// And after a very short while you will run out of stack space.
function DoSomething(bEnable) {
var myString = "Hello World";
if (bEnabled === true) {
DoSomething(true) // let's call ourself again
}
Debug.Trace('the end');
}
With such functions you can create very short code and/or do very fancy stuff. eg:
- many highly optimized sorting routines like
QuickSort routines are recursive.
- It is also used in many arithmetic functions, eg:
calculating the factorial of a number
- And of course it is used a hell of a lot for creating
Fractals.
However, there is one massive disadvantage with recursive functions: they eat memory... That is: each time the function calls itself again, the operating system needs to preserve everything and initialize the function again. This preserving is done by temporarly placing everything from that function in memory, the so call 'stack space'. Only when an iteration of a function is ended, the temporary used stack space for that individual iteration is freed. And the problem is that stack space is very limited...
So, if a function calls itself a bit too much, the operating system will run out of stack space, and you'll recieve that error...
And because Foaly's method uses such a recursive function, it has the potential danger of running out of stack space, as you just experienced.
This because it calls itself again each time it produces a random line which already exists in one of the x previous lines. I think I talked about this in one of my previous posts: This is mostly noticeable when the ratio between the number of unique lines and the total number of possible lines is very high. eg: you have 100 different lines to choose from and you don't want the same line to occur in a continues row of 99 lines. Foaly's function would in that case constantly call itself because the chance it picks an already existing line is extremely high (99/100). And the more the function calls itself, the more stack space it needs...
Thus when you use recursive functions you need to test and use them with great care.
PS: you can compare recursive functions with a 'todo list': After 5 minutes of being busy with a certain task, you start to work on another task and thus you put all the stuff from the first task in the 'busy' box. Then again after some time you start a third task, thus you put everything from task 2 in the 'busy' box on top of the stuff from the first task. Then after some times you start a 4th task, etc... Eventually the box will be full and you wont be able to put anything more in it...
quote:
Originally posted by stu
Also, it returned one undefined result, but I think that just has to do with the random number rounding up to a nonexistant number, as its using math.round in the code Volv provided, so thats no biggy to fix.
indeed, it should be math.floor... math.round should never be used to generate a random number.