Shoutbox

[Request] Random Number Generator - 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: [Request] Random Number Generator (/showthread.php?tid=91773)

[Request] Random Number Generator by cerealkiller54 on 08-05-2009 at 07:25 AM

Hello, all. :)

I would like to have a script that gives me a random number from an input number.
I.e. if I typed "/random 6" in the text box and sent it, it would give me a random number between 0 and 6.
And I don't want 6 to be the only number that can be randomized, I want the number after /random # to be a user input.

Any help? :^)

-------------------
xxxx says: 4
xxxx says: 8
xxxx says: 12
xxxx says: 0
xxxx says: 6
xxxx says: 5
-------------------
/random 12
-------------------


RE: [Request] Random Number Generator by Matti on 08-05-2009 at 08:16 AM

There are already many scripts which can do that, have a look in the scripts database.

The only problem I see is that you want a random number in the range of 0..n, whereas most dice roller scripts work in the range of 1..n (with n the upper limit). You could get around this by using a more advanced dice rolling formula, for example to roll between 0 and 12 you could use this command for Advanced Dice Roller:

code:
/roll 1d13-1
What I did here is increase the amount of sides of the dice by 1 and afterwards substract 1 from the result, resulting in a number between 0 and 12 instead of 1 and 12. This might be a bit complicated and long to type every time you need it, but you can make it a bit easier for you by storing your most used rolls in a Plus! quick text (Preferences > Conversations > Quick Texts > New), for example:
quote:
Alias: /random12
Text: /dice 1d13-1
Then you can simply type /random12 every time you want a random number between 0 and 12. If that doesn't satisfy you, you could also modify the script itself, but that's not really recommended.
RE: [Request] Random Number Generator by cerealkiller54 on 08-05-2009 at 06:30 PM

Wow thanks a bunch! :D

I already searched the script database, but I literally used the search function and I guess I was looking up the wrong keywords (number,random).


RE: [Request] Random Number Generator by matty on 08-05-2009 at 07:15 PM

Javascript code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage){
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        var Command = RegExp.$1.toLowerCase();
        var Param = parseFloat(RegExp.$2);
        switch (Command) {
            case 'random':
                if (Param !== 'NaN') {
                    return Math.floor((Param)*Math.random());;
                } else {
                    return '';
                }
        }
    }  
    return sMessage;
}


Something like that lol
RE: [Request] Random Number Generator by Matti on 08-05-2009 at 07:32 PM

Ah, matty goes the DIY way again! :P

However, I think you have a small error in your error handling. When parseFloat can't convert Param into a valid floating point number, it'll return NaN. Now, NaN isn't just a string containing those three letters, it's a special value and - ironically - is of the type Number.

The correct way to see whether the result is valid is by using the isNaN function provided by JScript/JavaScript. I also recommend doing the parseFloat call after confirming that the /random command was sent. So, this would be the corrected code:

Javascript code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage){
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        var Command = RegExp.$1.toLowerCase();
        var Param = RegExp.$2;
        switch (Command) {
            case 'random':
                if (!isNaN(Param = parseFloat(Param))) {                    return Math.floor((Param)*Math.random());;
                } else {
                    return '';
                }
        }
    }  
    return sMessage;
}

Yes, that line is perfectly valid. First I re-assign Param to the parseFloat result, then that result is checked against isNaN. Neat, uh? :P
RE: [Request] Random Number Generator by matty on 08-05-2009 at 07:55 PM

Wow shows how rusty I am... good catch my young padwan.