Shoutbox

[Question] Randomizing captions - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Question] Randomizing captions (/showthread.php?tid=62046)

[Question] Randomizing captions by pollolibredegrasa on 06-29-2006 at 07:04 PM

Edit: I meant radio control

Just a quick question (I'm just learning Jscript, so be nice), here's what I have:

1 Window with 4 radio controls defined in my XML file (Resp0, Resp1, Resp2, Resp3)

1 array (thecapt) with 10 strings called ("Blah 1", "Blah 2", "Blah 3", "Blah 4", "Blah 5" etc...)

What I need is to be able to load 4 strings from this array into the 4 radio controls at runtime - at the moment I can load the first 4 strings, however for the script to work the strings from the array need to be randomly selected (and not repeated in another radio control). I don't know how to explain really so heres a kinda example..

Run 1:

    O Blah 1
    O Blah 3
    O Blah 4
    O Blah 2

Run 2:

    O Blah 3
    O Blah 4
    O Blah 2
    O Blah 1

Etc... (each O is supposed to represent a radio control btw)

At the moment the code to fill the radio controls (in order) is as follows:

code:
    iwnd = MsgPlus.CreateWnd("Interface.xml","WndReply")
   
    var i
    for (i=0; i< 4; i++)
    {
        iwnd.SetControlText ("Resp"+[i], thecapt[i])
    }


Any help on how to do this is appreciated, I'm NOT asking you to write the code for me (unless you really want to :P) but any pointers in the right direction as to how to go about this would be appreciated :)

Ps:

I also have this code in the script to generate a random number between 0 and a user defined highest number. Dunno if this is of any use in my problem, but I'll include it here anyway:

code:
function RandNo(TopNo)
{
    return Math.floor(Math.random() * TopNo);
}

RE: [Question] Randomizing captions by RaceProUK on 06-29-2006 at 09:39 PM

The random element will be easy to code: just multiply the result of Math.random() by the length of the array. As for enuring no duplicates, try this:
1. Make a copy of the array, prefereably as a list or set.
2. Select an item from the copy at random.
3. use item.
4. Delete selected item from copy.
5. Repeat from 2.


RE: [Question] Randomizing captions by pollolibredegrasa on 06-29-2006 at 10:57 PM

Wow thanks! That method would be great.

One more quick question though, how do you remove a certain item from an array? i.e. after selecting the item at random,  how would I go about removing it from the array?

Thanks for the help :)


RE: [Question] Randomizing captions by RaceProUK on 06-30-2006 at 11:49 AM

I don't know about from an array, which is why I suggested a list or set. Not sure if they exist in JScript though: MSDN will have the answers, and if not, Google.


RE: [Question] Randomizing captions by pollolibredegrasa on 06-30-2006 at 01:43 PM

Well thanks for the help anyway :)

I loked through the MSDN library and didnt find anything about removing certain items from and array, but found a couple that returns (x) items from whatever start position you choose, as well as one that joins 2 or more arrays.

Using this I created a function that loops through the copy of the array until it finds the item to delete (storing each item in a third, temporary array). Then when it reaches the item to delete, it just adds all the remaining items after that to the temporary array, thus creating a copy of the array with the item removed :d

There are probably more effective methods, but this one seems to work, so I'm happy :)