This functions makes getting random whole/decimal numbers easy. It also automatically calls Randomize the first time the function is called, so there is no need to put it in the Form_Load Procedure.
VB Code:
' Place this code in a module. '''''''''''''''''''''''''''''''''' ' Date: 7/2/2005 ' ' Last Update: 7/29/2005 ' ' Author: Jeremy Blanchard ' '''''''''''''''''''''''''''''''''' Option Explicit Public Function Rand(Max As Double, _ Optional Min As Double = 0, _ Optional NumDigitsAfterDecimal As Integer = 0) As Double ' Returns a pseudo-random number between 'Min' and 'Max'. ' Calls Randomize the first time the function is called. Static sbRandomized As Boolean Dim r As Double If Not sbRandomized Then Randomize sbRandomized = True End If If NumDigitsAfterDecimal <= 0 Then Rand = Int(Rnd * (Max - Min + 1)) + Min Else ' Less accurate number distrubtion because of decimals. Rand = Round((Rnd * (Max - Min)) + Min, NumDigitsAfterDecimal) End If End FunctionNOTE: Update on 7/29/05 switched max to the first parameter and Min to the second (and made it default optional).VB Code:
' Example uses ... r = Rand(255) g = Rand(255) b = Rand(255) Form1.BackColor = RGB(r, g, b) ' Random background color ... iMyNum = Rand(10, -10, 3) ' Rounded to 3 decimal places
Let me know if you like this module or used it in a cool way.




Reply With Quote