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=69047)

[Request] Random Number Generator by Slacker on 12-01-2006 at 04:47 PM

Hello,

I would really apreciate if someone would be kind enough to make a simple script that generates a random number. When i say random number, a number between 1 and 100 would be ideal please. As im not 100% sure what the number bounds im going to need are, yet. But i'm sure i'll be able to figure out how to edit that tiny bit of the script.

Thanks.


RE: [Request] Random Number Generator by CookieRevised on 12-01-2006 at 07:44 PM

quote:
Originally posted by Slacker
I would really apreciate if someone would be kind enough to make a simple script that generates a random number. When i say random number, a number between 1 and 100 would be ideal please. As im not 100% sure what the number bounds im going to need are, yet. But i'm sure i'll be able to figure out how to edit that tiny bit of the script.
Always search first, and you will find...



Either search the forums:
quote:
CookieRevised's reply to '[I help them] VB2JS'
code:
// Returns a random number
function Rnd() { return Math.random() }

// Returns a random integer number in a range
//   lowerbound    (required) Specifies the lower limit of the range (limit inclusief)
//   upperbound    (required) Specifies the upper limit of the range (limit inclusief)

function RndBetween(lowerbound, upperbound) {
        lowerbound = Math.min(lowerbound, upperbound);
        upperbound = Math.max(lowerbound, upperbound);
        return Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound);
}


Or search the help file of Jscript:
http://www.msgpluslive.net/scripts/view/152-Windo...ipt-Documentation/






----------

quote:
Originally posted by -!Felu!-
code:
Math.floor(Math.random()*100)

That will not return a number in the range 1 to 100...  It will return a random number from 0 to 99.

To return a number in the range 1 to 100, you'll need:
Math.floor((100 - 1 + 1) * Math.random() + 1);
or
Math.floor(Math.random() * 100 + 1);




RE: RE: [Request] Random Number Generator by Jesus on 12-02-2006 at 01:17 AM

quote:
Originally posted by CookieRevised
That will not return a number in the range 1 to 100...  It will return a random number from 0 to 99.

To return a number in the range 1 to 100, you'll need:
Math.floor((100 - 1 + 1) * Math.random() + 1);
or
Math.floor(Math.random() * 100 + 1);

^o)
quote:
Math.floor((100 - 1 + 1) * Math.random() + 1);
equals
Math.floor(100 * Math.random() + 1);

which is about the same as
quote:
Math.floor(Math.random() * 100 + 1);
both will do the job, but... I didn't expect you to be this sloppy cookie, in your average posts you wouldn't use -1+1...
getting sleepy?? ;)


RE: [Request] Random Number Generator by foaly on 12-02-2006 at 01:25 AM

quote:
Originally posted by Jesus
I didn't expect you to be this sloppy cookie, in your average posts you wouldn't use -1+1...
getting sleepy?? (Smilie)
I guess that is to make it easier to understand :)

quote:
Originally posted by Jesus
I'd use Math.floor(Math.random() * 101); tbh(Smilie)
Then you still can get 0...

shouldn't it be:
Math.floor(Math.random() * 99 + 1);?

RE: [Request] Random Number Generator by CookieRevised on 12-02-2006 at 01:28 AM

:|

* CookieRevised slaps people not properly reading a plain simple post around with a large trout

:D




quote:
Originally posted by Jesus
both will do the job, but... I didn't expect you to be this sloppy cookie, in your average posts you wouldn't use -1+1... getting sleepy??
Jesus, read what -!Felu!- has written, then read my post again, properly... There is absolutely nothing sloppy about my post. ^o)



--

quote:
Originally posted by Jesus
I'd use Math.floor(Math.random() * 101); tbh(Smilie)
Then you're wrong. It will generate random numbers from 0 to 100.

See my previous post.

quote:
Originally posted by foaly
shouldn't it be: Math.floor(Math.random() * 99 + 1)?
No, that will generate numbers from 1 to 99.

See my previous post.








it should be
   Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound);
thus
   Math.floor((100 - 1 + 1) * Math.random() + 1);
thus
   Math.floor(Math.random() * 100 + 1);

See my previous post


:P
RE: RE: [Request] Random Number Generator by Jesus on 12-03-2006 at 02:21 AM

quote:
Originally posted by CookieRevised
quote:
Originally posted by Jesus
both will do the job, but... I didn't expect you to be this sloppy cookie, in your average posts you wouldn't use -1+1... getting sleepy??
Jesus, read what -!Felu!- has written, then read my post again, properly... There is absolutely nothing sloppy about my post. ^o)
I don't see any post by -!Felu!- in this thread...
maybe he deleted it?

quote:
quote:
Originally posted by Jesus
I'd use Math.floor(Math.random() * 101); tbh(Smilie)
Then you're wrong. It will generate random numbers from 0 to 100.
quote:
Originally posted by foaly

Then you still can get 0...

true, I noticed that and corrected it as you were typing ;)

quote:
[b]it should be
   Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound);
thus
   Math.floor((100 - 1 + 1) * Math.random() + 1);
thus
   Math.floor(Math.random() * 100 + 1);

Sorry, I never saw you or anyone else post the Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound); part. that would have made it a lot clearer ;)

at least we agree on the Math.floor(Math.random() * 100 +1); part ;)
RE: [Request] Random Number Generator by CookieRevised on 12-03-2006 at 05:44 AM

quote:
Originally posted by Jesus
I don't see any post by -!Felu!- in this thread...
maybe he deleted it?
Felu was the first one to post a reply to this thread and which I also quoted in my post. His post was only deleted long after you replied to my post... So you could have seen that.

quote:
Originally posted by Jesus
Sorry, I never saw you or anyone else post the Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound); part. that would have made it a lot clearer
The "Math.floor((upperbound - lowerbound + 1)..." is also in my original post, clearly visible... You could have seen that too.

You first replied in this thread 6 hours after my last edit of my original reply, so don't say you missed something because it wasn't there or because I edited in afterwards

:p
RE: RE: [Request] Random Number Generator by Jesus on 12-03-2006 at 10:37 AM

quote:
Originally posted by CookieRevised
Felu was the first one to post a reply to this thread and which I also quoted in my post. His post was only deleted long after you replied to my post... So you could have seen that.

The "Math.floor((upperbound - lowerbound + 1)..." is also in my original post, clearly visible... You could have seen that too.

You first replied in this thread 6 hours after my last edit of my original reply, so don't say you missed something because it wasn't there or because I edited in afterwards

:p

lol you're right, it's in the quote part :P
I guess it's just that I would have repeated it just before working it out to make clear why there is a pointless addition and subtraction in one line of code. Just like you did in the other post:
quote:
Originally posted by CookieRevised
[b]it should be
   Math.floor((upperbound - lowerbound + 1) * Math.random() + lowerbound);
thus
   Math.floor((100 - 1 + 1) * Math.random() + 1);
thus
   Math.floor(Math.random() * 100 + 1);

I just noticed it looked weird and didn't immediately see why you used it, that's all ;)