Wednesday, February 10, 2010

Random Number

RAND()
- return real number that >=0 and < 1
RANDBETWEEN(bottom,top)
- return integer in specified range

Note: every Excel calculations, random number will change, to fix the value generated
-> enter =rand() in formula bar and press F9

For example
  1. =rand() => ex. 0.135
  2. =rand()*10 => return real number that >=0 and < 10
  3. =int(rand()*10) => return integer that >=0 and < 10
  4. =randbetween(1,10) => return integer that >=1 and <=10 (ex. 7)

Random in VBA
RND()
- return single value that >=0 and < 1

Int(Rnd * (bottom- top+ 1)+ bottom)
- return integer in specific range

For example
--------------
Sub test()
Randomize
MsgBox Rnd()
End Sub
=> return single number ex. 0.741725
--------------
Sub test2()
'to find random integer in range between 1 and 10
Dim RandNo As Integer
Randomize
RandNo = Int(Rnd() * 10 + 1) '10 = 10-1+1
MsgBox RandNo
End Sub
=> return integer that >=1 and <=10 ex. 6
--------------

Note: single value is ranging in value from -3.402823E38 to -1.401298E-45 for negative values and from 1.401298E-45 to 3.402823E38 for positive values

No comments:

Post a Comment