What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Request] Random Number Generator

[Request] Random Number Generator
Author: Message:
cerealkiller54
New Member
*


Posts: 2
Joined: Aug 2009
O.P. [Request] Random Number Generator
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
-------------------
08-05-2009 07:25 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Request] Random Number Generator
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.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-05-2009 08:16 AM
Profile E-Mail PM Web Find Quote Report
cerealkiller54
New Member
*


Posts: 2
Joined: Aug 2009
O.P. RE: [Request] Random Number Generator
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).
08-05-2009 06:30 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: [Request] Random Number Generator
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
08-05-2009 07:15 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Request] Random Number Generator
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
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-05-2009 07:32 PM
Profile E-Mail PM Web Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: [Request] Random Number Generator
Wow shows how rusty I am... good catch my young padwan.

This post was edited on 08-05-2009 at 07:55 PM by matty.
08-05-2009 07:55 PM
Profile E-Mail PM Find Quote Report
« 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