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: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Random numbers, prevent from being used more than once
I think the problem you have is that you're thinking too much about seperate arrays and making it so that your code switches between arrays, checks stuff, etc.... You're focussing too much on that. Forget about all that. You only need 1 array to work with, nothing more. And that array should only be (re)created when the user enables the "change PSM to random line" feature of your script.

What you need to do in the main code is getting a random line out of 1 array. What that array contains is of no concearn anymore as that array was created and filled at the beginning of your script (aka: when the user changed options). As such it could contain quotes, lyrics, the lines from 5 external files, only the lines from 1 external file, or whatever else you (or the user) wants, combinations of stuff, it doesn't matter.

1) script initializes (or user enabled the feature of the script or whatever): create the array.
2) When timer triggers and PSM must change: read random line from the array. No need to check anything, you already have your array...
;)


quote:
Originally posted by stu
quote:
in that case you do not need the which checking, seperate arrays, and all that other stuff at all. Just concat the lyrics array with your quotes array and be done with it. In other words, you're REALLY making it WAY hard on yourself ...

(and hence my initial suggestion to not alter my example routine at all but instead optimizing your code!).
Yes, I could very well do that, and the reason I did not was that if, going by my example again, I used lyrics, and jokes, they would be displayed different, by adding (8) in front of the lyric, and probably not adding anything to the joke. If i just had them all together, the jokes would also have (8) in front of them.. that is why they are seperate...
No it wouldn't.

That's what I mean with "you're thinking to much about seperate arrays"...

If you want to show "(8)" in front of lyrics, then add "(8)" to each lyric line when you create the array, not when you are showing stuff...

So the array would contain:
"quote one"
"quote two"
"(8) lyric one"
"(8) lyric two"
":) joke one"
":) joke two"

You don't need seperate arrays....

In that same matter you can instead add 1 character as an identifiers to each line you add to the array to indicate 'special stuff' to be done when you actually show a line (eg: "Q" for a quote, "L" for a lyric, etc). The point is: you're doing stuff inside the "show it" routine which should actually be done before you show it, not during it...

;)

This post was edited on 07-05-2007 at 02:46 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-05-2007 02:32 AM
Profile PM Find Quote Report
Volv
Skinning Contest Winner
*****

Avatar

Posts: 1233
Reputation: 31
34 / Male / Flag
Joined: Oct 2004
RE: Random numbers, prevent from being used more than once
quote:
Originally posted by CookieRevised
Actualy foaly's method is not going to work
It works perfectly fine for generating the random number without repetition until 10 others have been selected.
quote:
Originally posted by CookieRevised
foaly's method doesn't return anything if the randomly generated lyric already exists in the past 10 lines! His function would simply stop.
Yes it does, it's a recursive function if you look closely. If the line exists it simply calls itself again until it returns a new number.
quote:
Originally posted by CookieRevised
quote:
Although I think instead of:
count=count%9
You should replace it with:
count= (count==9 ? 0:count);

Because you don't want to have unnecessarily large integers being stored in memory.
it wouldn't make a difference.
I already said this in the edit about 8 hours ago =/


Granted, the theory behind your idea is much more intelligent and efficient you shouldn't be dissing people's suggestions without first understanding them :(
07-05-2007 02:45 AM
Profile PM Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
Ok, true enough, if I want to take the extra time to change that now, and each future one as I add it. I am just going to leave that for now though. As I said, I have now cut out the which statement alltogether, and am using just the Text array so its not a huge deal right now, to me..

I still cant get this to work though, when it calls the GetRandomLyric function,nothing seems to happen, and it just returns "undefined"..

Edit: Looks like I was missing a line of code, my bad.. testing it out now..

quote:
quote:
--------------------------------------------------------------------------------
Originally posted by CookieRevised
Actualy foaly's method is not going to work
--------------------------------------------------------------------------------


It works perfectly fine for generating the random number without repetition until 10 others have been selected.

I tried his method as well, but could not get that to work. If you think it can, that would be great if you could think of a way for me to figure it out, always better to know more than one method..
___________________________________________________________

Ok, so I got it to display a lyric. when the lyric is chosen, it is removed from the array, and added into arrPrevious (but it only seems to stay there for one lyric, see debuging). After 10 lyrics though, they dont get added back into the original array. Here is what the script debugger shows.. As you can see, my Text array consists of 12 Tests..
quote:
Started Personal Message
Printed personal message - Test 10
ArrPrevious - ,,,,,,,,Test 10
ArrLyrics - Test 1,Test 2,Test 3,Test 4,Test 5,Test 6,Test 7,Test 8,Test 9,Test 11,Test 12
Started Personal Message
Printed personal message - Test 8
ArrPrevious - ,,,,,,,,Test 8
ArrLyrics - Test 1,Test 2,Test 3,Test 4,Test 5,Test 6,Test 7,Test 9,Test 11,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 9
ArrPrevious - ,,,,,,,,Test 9
ArrLyrics - Test 1,Test 2,Test 3,Test 4,Test 5,Test 6,Test 7,Test 11,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 5
ArrPrevious - ,,,,,,,,Test 5
ArrLyrics - Test 1,Test 2,Test 3,Test 4,Test 6,Test 7,Test 11,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 11
ArrPrevious - ,,,,,,,,Test 11
ArrLyrics - Test 1,Test 2,Test 3,Test 4,Test 6,Test 7,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 2
ArrPrevious - ,,,,,,,,Test 2
ArrLyrics - Test 1,Test 3,Test 4,Test 6,Test 7,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 1
ArrPrevious - ,,,,,,,,Test 1
ArrLyrics - Test 3,Test 4,Test 6,Test 7,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 4
ArrPrevious - ,,,,,,,,Test 4
ArrLyrics - Test 3,Test 6,Test 7,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 7
ArrPrevious - ,,,,,,,,Test 7
ArrLyrics - Test 3,Test 6,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 3
ArrPrevious - ,,,,,,,,Test 3
ArrLyrics - Test 6,Test 12
Timer = 15000
Started Personal Message
Printed personal message - Test 12
ArrPrevious - ,,,,,,,,Test 12
ArrLyrics - Test 6
Timer = 15000
Started Personal Message
Printed personal message - Test 6
ArrPrevious - ,,,,,,,,Test 6
ArrLyrics -
Timer = 15000
Started Personal Message
Printed personal message -
ArrPrevious - ,,,,,,,,
ArrLyrics -
Timer = 15000

This post was edited on 07-05-2007 at 03:07 AM by stu.
07-05-2007 02:51 AM
Profile E-Mail PM Web Find Quote Report
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 Volv
quote:
Originally posted by CookieRevised
Actualy foaly's method is not going to work
It works perfectly fine for generating the random number without repetition until 10 others have been selected.
sorry for missing the recursive bit.
quote:
Originally posted by Volv
I already said this in the edit about 8 hours ago =/
quote:
(though I didn't posted this yet because I was at work all day)

quote:
Originally posted by Volv
you shouldn't be dissing people's suggestions without first understanding them :(
ermm.... first of all I'm not dissing anything, I'm trying to explain things. Big difference... Second, yes, I missed the recursive bit of foaly's method, but that is still not dissing or not understanding stuff. Seeing all the stuff I wrote in this thread I think it is clear I understand how the suggested stuff works. In fact, in that same first post of mine you quoted from, I also explained foaly's method and what disadvantage it has. The reason I missed the recursive bit was not because I don't understand what he wrote, it was because it I saw what "method" he used, not what exact "code" he wrote (hard to explain though).

If I would be dissing stuff I wouldn't take the hours of time I already put into this to explain stuff... I would simply say it is crap or something (and thus wouldn't say anything at all)...

:/



quote:
Originally posted by stu
Ok, true enough, if I want to take the extra time to change that now, and each future one as I add it.
You don't need to change all your saved files, you can do it inside the code when you create the array... If you read lines from a "jokes" file, add the smilie, if it is from a "lyrics" file, add the note, etc...

quote:
Originally posted by stu
Ok, so I got it to display a lyric. when the lyric is chosen, it is removed from the array, and added into arrPrevious (but it only seems to stay there for one lyric, see debuging). After 10 lyrics though, they dont get added back into the original array. Here is what the script debugger shows.. As you can see, my Text array consists of 12 Tests..
You've not copied the code exactly as I put it (or you changed stuff too much or in the wrong way)...

This post was edited on 07-05-2007 at 04:03 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
07-05-2007 03:07 AM
Profile PM Find Quote Report
Volv
Skinning Contest Winner
*****

Avatar

Posts: 1233
Reputation: 31
34 / Male / Flag
Joined: Oct 2004
RE: Random numbers, prevent from being used more than once
quote:
Originally posted by stu
I tried his method as well, but could not get that to work. If you think it can, that would be great if you could think of a way for me to figure it out, always better to know more than one method..
code:
var arrLyrics = Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

var count = 0;
var arrUsedLyrics = new Array();
function GetIndex(arrArray, intRepeatAfter) {
    var i = Math.round(Math.random()*arrArray.length);
   
    for(var j=0;j<arrUsedLyrics.length;j++){
          if(i==arrUsedLyrics[j]){
            return GetIndex(arrArray,intRepeatAfter);
          }
    }
   
    arrUsedLyrics[count]=i;
    count++;
    count=count%(intRepeatAfter-1);
    return i;
}
I don't take credit for this code - foaly's idea.
and whenever you want to pick a random element from an array you just do:
var myRandomLyrics = arrLyrics[GetIndex(arrLyrics,10)];
Where 10 is the number of lyrics after one has been chose which must be chosen before that lyric can be repeated.

NOTE: You must still only ever have 1 array (as Cookie has pointed out) otherwise you will bump into problems.

This post was edited on 07-05-2007 at 03:13 AM by Volv.
07-05-2007 03:11 AM
Profile PM Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
Thanks Volv for the (foaly's) code, I'll try that out as well once I get Cookie's code working
___________________________________

quote:
You've not copied the code exactly as I put it (or you changed stuff too much or in the wrong way)...
Perhaps so, take a look.

This is the code as I have it.. well, the main functions...
code:
var Text=new Array();

var num=0;

Text[num++] = "Test 1";

Text[num++] = "Test 2";

Text[num++] = "Test 3";

Text[num++] = "Test 4";

Text[num++] = "Test 5";

Text[num++] = "Test 6";

Text[num++] = "Test 7";

Text[num++] = "Test 8";

Text[num++] = "Test 9";

Text[num++] = "Test 10";

Text[num++] = "Test 11";

Text[num++] = "Test 12";


///////////////////////////////////////////////////////////
//                    Script Starts here
//////////////////////////////////////////////////////////

var arrLyrics = new Array();
var arrPrevious = new Array();
var nUniqueness = 10;


function GetAllLyrics() {
arrPrevious = new Array();
return Text
}

function GetRandomLyric() {
// get a random lyric line from the 'possible lyrics' array
var rndLyric = Math.floor(Math.random() * arrLyrics.length);
// step 1: add new line to 'used lyrics' array and remove it from the 'possible lyrics' array
arrPrevious[nUniqueness-1] = arrLyrics.splice(rndLyric, 1);
// step 2: add last lyric line from the 'used lyrics' array back to the 'possible lyrics' array
if (arrPrevious[0] !== undefined) arrLyrics.push(arrPrevious[0]);
// step 3: remove last lyric line from the 'used lyrics' array
arrPrevious.shift();
// you're done: return the new lyric line
return arrPrevious[nUniqueness-2]
Debug.Trace(arrPrevious);
}


function personalMessage()
{
Debug.Trace ("Started Personal Message");
    //reading the timer value from the registry
    try{
        timer = WShell.RegRead(MsgPlus.ScriptRegPath+Messenger.MyUserId+"\\JeremysRandomSongLyricTimer");
    }
    catch(e){}

    try{
        randomlyric = WshShell.RegRead(MsgPlus.ScriptRegPath+Messenger.MyUserId+"\\JeremysRandomSongLyric");
    }
    catch(e){}
    if (randomlyric==1)
    {

       
        arrLyrics = GetAllLyrics();

        var rndLyric = GetRandomLyric();
        //Messenger.MyPersonalMessage = "( 8)" + rndLyric;
        MsgPlus.DisplayToast("New Random Lyric", "( 8)" + rndLyric, null, 'OnToastClick', null);
        MsgPlus.AddTimer('randomtext', timer);
        Debug.Trace ("Printed personal message - " + rndLyric);
        Debug.Trace ("ArrPrevious - " + arrPrevious);
        Debug.Trace ("ArrLyrics - " + arrLyrics);
       
}

This post was edited on 07-05-2007 at 03:28 AM by stu.
07-05-2007 03:22 AM
Profile E-Mail PM Web Find Quote Report
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
remove
quote:
Originally posted by stu
arrLyrics = GetAllLyrics();
Do not (re)create/(re)read the array each time you wanna change the PSM. The array should be created only once, outside of personalMessage()...

That's the major point I'm trying to make in all my posts...

Instead put that line in the initializing code of your script or in the part where the user activates the PSM changing for the first time (if that is still in the code).

;)


PS:
code:
// you're done: return the new lyric line
return arrPrevious[nUniqueness-2]
Debug.Trace(arrPrevious);
That debug line will never be executed since the function is ended before that line because of the statement 'return'.

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

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
Removing that line prevents a lyric from being displayed though, even when changing the array Text to ArrLyrics.. all I get then is undefined.
I realize that debug line will not be executed, but the debug lines that I have added inside the personalMessage function now display nothing for arrPrevious, and still show the array entries for arrLyrics
07-05-2007 03:45 AM
Profile E-Mail PM Web Find Quote Report
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
Removing that line prevents a lyric from being displayed though, even when changing the array Text to ArrLyrics..
Because you need to create the array first.

Put that line in the initializing code of your script or in the part where the user activates the PSM changing for the first time (if that is still in the code). Thus outside of personalMessage()...

again (no offense though!):

You're constantly doing:
1) timer triggerd
        I) make new PSM
                a) create different arrays
                b) get random line from array

You need to do:
1) create 1 array
2) timer triggerd
        I) make new PSM
                a) get random line from array

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

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. RE: Random numbers, prevent from being used more than once
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.. So that should be put in OnEvent_Signin(Email), and OnEvent_Initialize(MessengerStart). (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?)

Edit: I think I would need to add it to the toggle function, that switches between on and off, as well *-)

Is my thinking now correct? I think this is all beginning to make sense to me now, lol. I really must thank you for all this help :)

Edit2: So it does seem to work now, I added it to the three places mentioned above. It does make more sense having it that way, it just took a while to get it through my head, lol. Sorry for the difficulties, I blame it on myself currently being sick and not functioning properly, lol.

This post was edited on 07-05-2007 at 04:13 AM by stu.
07-05-2007 03:55 AM
Profile E-Mail PM Web 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