Home > Archive > VBScript > November 2004 > Math problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I'm working on a script that requires the computer to chose a random number,
either 1, 2 or 3. I looked in a vbscript source book and it gives the
following example:
Randomize
GetRandomNumber = Int((3 * Rnd( )) + 1)
The author says the Rnd( ) function generates a random number between 0 and
1. The Int( ) function, which returns the integer portion of a number,
executes, multiplying 3 times the randomly generated number and then adds 1
to it. The final result is a randomly generated number with a value between
1 and 3.
If I do the math 3 times 0 is 0 and add 1 is 1. 3 times 1 is 3 plus 1 is 4.
Am I not understanding something in this equation? If not I would
appreciate an explanation. Thanks, Jim
| |
| y sakuda 2004-11-23, 3:55 am |
| Please don't repeat same post.
It take several minutes to appear on server.
"JimT" <someone@comcast.com> wrote in message news:%23rv2laQ0EHA.2200@TK2MSFTNGP09.phx.gbl...
> I'm working on a script that requires the computer to chose a random number,
> either 1, 2 or 3. I looked in a vbscript source book and it gives the
> following example:
>
> Randomize
> GetRandomNumber = Int((3 * Rnd( )) + 1)
>
> The author says the Rnd( ) function generates a random number between 0 and
> 1. The Int( ) function, which returns the integer portion of a number,
> executes, multiplying 3 times the randomly generated number and then adds 1
> to it. The final result is a randomly generated number with a value between
> 1 and 3.
>
> If I do the math 3 times 0 is 0 and add 1 is 1. 3 times 1 is 3 plus 1 is 4.
> Am I not understanding something in this equation? If not I would
> appreciate an explanation. Thanks, Jim
>
>
| |
|
| I thought I had deleted the first post, this is the only one that shows up
here. Please respond to this post and ignore the first one. Thanks, Jim
"y sakuda" <sakudayasuichiro@hotmail.com> wrote in message
news:OCbDQdQ0EHA.2860@TK2MSFTNGP15.phx.gbl...[color=darkred]
> Please don't repeat same post.
> It take several minutes to appear on server.
>
> "JimT" <someone@comcast.com> wrote in message
> news:%23rv2laQ0EHA.2200@TK2MSFTNGP09.phx.gbl...
| |
| y sakuda 2004-11-23, 3:55 am |
| "JimT" <someone@comcast.com> wrote in message news:%23rv2laQ0EHA.2200@TK2MSFTNGP09.phx.gbl...
> I'm working on a script that requires the computer to chose a random number,
> either 1, 2 or 3. I looked in a vbscript source book and it gives the
> following example:
>
> Randomize
> GetRandomNumber = Int((3 * Rnd( )) + 1)
>
> The author says the Rnd( ) function generates a random number between 0 and
> 1. The Int( ) function, which returns the integer portion of a number,
> executes, multiplying 3 times the randomly generated number and then adds 1
> to it. The final result is a randomly generated number with a value between
> 1 and 3.
>
> If I do the math 3 times 0 is 0 and add 1 is 1. 3 times 1 is 3 plus 1 is 4.
> Am I not understanding something in this equation? If not I would
> appreciate an explanation. Thanks, Jim
>
You ask why GetRandomNumber do'nt get 4?
Because Rnd() always under 1. (never reach 1.0)
0 < Rnd() < 1
(Maybe Rnd() return 0, I don't know precisely)
Y Sakuda from JPN
| |
| Richard Mueller [MVP] 2004-11-23, 3:56 pm |
|
"y sakuda" <sakudayasuichiro@hotmail.com> wrote in message
news:%237KU6mQ0EHA.3072@TK2MSFTNGP11.phx.gbl...
> "JimT" <someone@comcast.com> wrote in message
news:%23rv2laQ0EHA.2200@TK2MSFTNGP09.phx.gbl...
number,[color=darkred]
and[color=darkred]
adds 1[color=darkred]
between[color=darkred]
is 4.[color=darkred]
>
> You ask why GetRandomNumber do'nt get 4?
> Because Rnd() always under 1. (never reach 1.0)
> 0 < Rnd() < 1
> (Maybe Rnd() return 0, I don't know precisely)
> Y Sakuda from JPN
>
Per Tim Hill's "Windows 2000 Windows Script Host", the Rnd function returns
a number between 0 and 1, including 0, but not 1.
0 <= Rnd() < 1
--
Richard
Microsoft MVP Scripting and ADSI
HilltopLab web site - http://www.rlmueller.net
--
|
|
|
|
|