What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Random numbers, prevent from being used more than once

Random numbers, prevent from being used more than once
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Random numbers, prevent from being used more than once
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 debugger
code:
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.


;)
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-05-2007 11:39 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Random numbers, prevent from being used more than once - by stu on 07-03-2007 at 10:14 PM
RE: Random numbers, prevent from being used more than once - by pollolibredegrasa on 07-03-2007 at 10:33 PM
RE: Random numbers, prevent from being used more than once - by foaly on 07-03-2007 at 10:34 PM
RE: Random numbers, prevent from being used more than once - by roflmao456 on 07-04-2007 at 03:15 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 06:01 AM
RE: Random numbers, prevent from being used more than once - by markee on 07-04-2007 at 02:17 PM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 02:31 PM
RE: Random numbers, prevent from being used more than once - by markee on 07-04-2007 at 02:35 PM
RE: Random numbers, prevent from being used more than once - by foaly on 07-04-2007 at 04:00 PM
RE: Random numbers, prevent from being used more than once - by stu on 07-04-2007 at 04:50 PM
RE: Random numbers, prevent from being used more than once - by roflmao456 on 07-04-2007 at 05:22 PM
RE: Random numbers, prevent from being used more than once - by Volv on 07-04-2007 at 05:47 PM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 12:34 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 12:51 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 12:55 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 01:03 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 01:32 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 01:55 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 02:00 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 02:18 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 02:32 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-05-2007 at 02:45 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 02:51 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:07 AM
RE: Random numbers, prevent from being used more than once - by Volv on 07-05-2007 at 03:11 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:22 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:36 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:45 AM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 03:47 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 03:55 AM
RE: RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 04:23 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 04:30 AM
RE: Random numbers, prevent from being used more than once - by foaly on 07-05-2007 at 10:57 AM
RE: Random numbers, prevent from being used more than once - by stu on 07-05-2007 at 04:59 PM
RE: Random numbers, prevent from being used more than once - by CookieRevised on 07-05-2007 at 11:39 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On