ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

¦Û¤v¼g ¦Û¤vªº¤è¦¡²£¥Í¶Ã¼Æ

¥»©«³Ì«á¥Ñ c_c_lai ©ó 2013-10-26 12:23 ½s¿è

¦^´_ 4# sunnyso
VBA Function Example

The RND function can only be used in VBA code in Microsoft Excel.
Here are some examples of what the RND function would return:
  1.     Int ((10 - 1 + 1) * Rnd + 1)     '   ¶Ç¦^¤¶©ó 1 ¨ì 10 ¶¡ªº¶Ã¼Æ­È
  2.     Int ((200 - 100 + 1) * Rnd + 100)   '  ¶Ç¦^¤¶©ó 100 ¨ì 200 ¶¡ªº¶Ã¼Æ­È
  3.     Int ((600 - 300 + 1) * Rnd + 300) '  ¶Ç¦^ ¤¶©ó 300 ¨ì 600 ¶¡ªº¶Ã¼Æ­È
½Æ»s¥N½X
For example:
  1. Sub Demo()
  2.     Dim retNum As Integer

  3.     retNum = Int ((300 - 200 + 1) * Rnd + 200)
  4.     MsgBox "retNum ªº¶Ã¼Æ­È = " & retNum
  5. End Sub
½Æ»s¥N½X
In this example, the variable called retNum would now contain a random number between 200 and 300.

TOP

¦^´_ 14# stillfish00
³o¬O§Ú´ú¸Õªºµ²ªG¡G
A.  rng = myRnd(sngVal)

B.  rng = myRnd(sngVal, 2 ^ 32)

²¤¥[¼W­×³¡¤À¡G
  1. Option Explicit

  2. Sub Ex()
  3.     Dim sngVal As Single, rng As Range, xi As Long
  4.    
  5.     With Sheets(1)
  6.         .Cells.Clear
  7.         
  8.         For xi = 1 To 5
  9.             Set rng = .Range(Chr(64 + xi) & "1")
  10.             For sngVal = 1 To 20
  11.                 '  rng = myRnd(sngVal)
  12.                 rng = myRnd(sngVal, 2 ^ 32)
  13.                 Set rng = rng.Offset(1)
  14.             Next sngVal
  15.         Next xi
  16.         .Columns.AutoFit
  17.     End With
  18. End Sub

  19. Function myRnd(Optional Number, Optional m As Double = 2 ^ 24) As Single
  20.     Dim tmp As Double
  21.     '  LCG Algorithm¡G
  22.     '      X(n+1) = (a * X(n) + c) mod m
  23.     Const a As Long = 1140671485
  24.     Const c As Long = 12820163
  25.     '  Const m As Long = 16777216    '  2^24
  26.     Const x0 As Long = 1         '  ®Ú¾Ú§A¨Ï¥Î­þ²Õ°Ñ¼Æ¡Ax0¦³®É¦³»Ý­n²Å¦Xªº±ø¥ó¡C
½Æ»s¥N½X

TOP

        ÀR«ä¦Û¦b : ¤H¥Í³Ì¤jªº¦¨´N¬O±q¥¢±Ñ¤¤¯¸°_¨Ó¡C
ªð¦^¦Cªí ¤W¤@¥DÃD