[Request] Random Number Generator |
Author: |
Message: |
Slacker
New Member
Posts: 5
Joined: Oct 2006
|
O.P. [Request] Random Number Generator
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.
|
|
12-01-2006 04:47 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Request] Random Number Generator
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);
This post was edited on 06-24-2008 at 05:33 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
12-01-2006 07:44 PM |
|
|
Jesus
Scripting Contest Winner
Koffie, my cat ;)
Posts: 623 Reputation: 15
38 / /
Joined: Jul 2005
|
RE: RE: [Request] Random Number Generator
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);
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??
This post was edited on 12-02-2006 at 01:23 AM by Jesus.
|
|
12-02-2006 01:17 AM |
|
|
foaly
Senior Member
Posts: 718 Reputation: 20
38 / /
Joined: Jul 2006
|
RE: [Request] Random Number Generator
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);?
|
|
12-02-2006 01:25 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Request] Random Number Generator
* CookieRevised slaps people not properly reading a plain simple post around with a large trout
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.
--
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
This post was edited on 12-02-2006 at 01:56 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
12-02-2006 01:28 AM |
|
|
Jesus
Scripting Contest Winner
Koffie, my cat ;)
Posts: 623 Reputation: 15
38 / /
Joined: Jul 2005
|
RE: RE: [Request] Random Number Generator
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.
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
|
|
12-03-2006 02:21 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Request] Random Number Generator
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
This post was edited on 12-03-2006 at 05:51 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
12-03-2006 05:44 AM |
|
|
Jesus
Scripting Contest Winner
Koffie, my cat ;)
Posts: 623 Reputation: 15
38 / /
Joined: Jul 2005
|
RE: RE: [Request] Random Number Generator
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
lol you're right, it's in the quote part
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
|
|
12-03-2006 10:37 AM |
|
|
|