Last active
August 29, 2015 14:04
-
-
Save mathew-kurian/de416af041345519c4b0 to your computer and use it in GitHub Desktop.
VBA Useful Random Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Test() | |
Debug.Print Random(0, 50) | |
Debug.Print RandomDateBetween(#1/1/2001#, #12/31/2001#) | |
Debug.Print RandomFrom(Array("cat", "dog")) | |
Debug.Print RandomDate | |
End Sub | |
Function RandomFrom(A As Variant) As String | |
RandomFrom = CStr(A(Random(0, UBound(A)))) | |
End Function | |
Function RandomDate() As Date | |
RandomDate = Int(Rnd() * CDbl(Date + 1)) | |
End Function | |
Function RandomDateBetween(LowerDate As Date, UpperDate As Date) As Date | |
RandomDateBetween = Int((UpperDate - LowerDate + 1) * Rnd + LowerDate) | |
End Function | |
Function Random(S As Integer, E As Integer) As Integer | |
Random = Int(Rnd * (E - S + 1)) + S | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment