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

Pages: (4): « First « 1 2 3 [ 4 ] Last »
Random numbers, prevent from being used more than once
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: RE: Random numbers, prevent from being used more than once
quote:
Originally posted by stu
lol, ok, so lets see if I get this right.. I should remove
code:
arrLyrics = GetAllLyrics();
from my personalMessage function, but add it in where the script is first initialized from..
yes

quote:
Originally posted by stu
So that should be put in OnEvent_Signin(Email), and OnEvent_Initialize(MessengerStart).

Edit: I think I would need to add it to the toggle function, that switches between on and off, as well *-)
Actually, you only need to add it once. Best to do this in the 'toggle' function.

The OnEvent_Signin() and OnEvent_Initialize() just need to point to the toggle function also. Because I suspect you currently have the same code in those functions also....

This is again optimizing code in a logic way, splitting things up, instead of putting many same code all over the place.

eg:
code:
function OnEvent_Initialize() {
        if (Messenger.MyStatus > 0) OnEvent_Signin();
}

function OnEvent_Signin() {
        GetUserSettings()
}

function OnEvent_SignOut() {
        SaveUserSettings();
        Toggle(false);
}

function GetUserSettings() {
        <read the user settings from the registry here, eg: should the stuff be enabled or not (= boolean value bEnabled below)>
        Toggle(bEnable);
}

function SaveUserSettings() {
        <write the user settings to the registry here>
}

function Toggle(bEnable) {
        if (bEnable) {
            arrLyrics = GetAllLyrics();
            <do some other stuff here, like starting timers or whatever>
        } else {
            <disable timers, etc>
        }
}

function personalMessage() {
        <...>
}
something like that...

In that way you only need to change stuff in one place.



quote:
Originally posted by stu
(Little bit off topic: I have copied most of this stuff from Toast Message, as it had a menu similiar to what I wanted, and then I tweaked it for myself. It has both of those functions doing the same thing, what exactly is the difference of them both, and do I really need both of them?)
You only need one of the two. The difference is only in what window it uses to get to the contactlist.

This post was edited on 07-05-2007 at 04:26 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-05-2007 04:23 AM
Profile PM Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
39 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
Ok, thanks a lot for the help :) Glad to finally get this working. I will work on optimizing my code further.. probably would explain why I couldnt get this working trying it myself, my array kept on getting erased, now I see why.

As for only having that line of code in one place, I believe I need to keep it in the 3 places, unless I cut out one of the two functions that do the same thing. Those two are setup totally different then the toggle function by reading the registry at the start to check if the script is turned on, and toggle switches between on and off.

Again, thanks for the help
07-05-2007 04:30 AM
Profile E-Mail PM Web Find Quote Report
foaly
Senior Member
****

Avatar

Posts: 718
Reputation: 20
38 / Male / Flag
Joined: Jul 2006
RE: Random numbers, prevent from being used more than once
Glad you got it to work...
If you're still going to try using "my" code (still can't believe I've got my own code :P.)
Be careful that you don't try to random a number lower then the repeating value.
This will cause an infinite loop and will crash your messenger...
(You're right cookie that the loop thingy is a big disadvantage...)
The best thing to do is sticking to cookies code :) 
07-05-2007 10:57 AM
Profile E-Mail PM Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
39 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
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
quote:
Error: Out of stack space (code: -2146828260)
       File: test.js. Line: 68.
which is this line
code:
return GetIndex(Text,intRepeatAfter);
 
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. But I think I am going to take your advice and just stick to Cookies code, so thanks a lot for the suggestion anyways :)
07-05-2007 04:59 PM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
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
Pages: (4): « First « 1 2 3 [ 4 ] Last »
« Next Oldest Return to Top Next Newest »


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