hey guys, anyone here have a function for returning a number between 2 given parameters etc..
Number(50,79)
would give me any randm number between 50-79
Printable View
hey guys, anyone here have a function for returning a number between 2 given parameters etc..
Number(50,79)
would give me any randm number between 50-79
Try the following sample:
VB Code:
Option Explicit Private Sub Command1_Click() Dim lngNewNum As Long lngNewNum = GenerateNumber(50, 79) Debug.Print lngNewNum End Sub Public Function GenerateNumber(lngNum1 As Long, lngNum2 As Long) As Long Randomize GenerateNumber = Int((lngNum2 * Rnd) + lngNum1) End Function
Make a Function() using this formula:
VB Code:
'To produce random integers in a given range, use this formula: Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
thanks but it doesnt work it give me numbers like 2500 whe i tryQuote:
Originally Posted by RhinoBull
GenerateNumber(1900, 1990)
VB Code:
Option Explicit Private Sub Form_Load() Randomize End Sub Private Sub Command1_Click() MsgBox GenRnd(1900, 1990) End Sub Private Function GenRnd(ByVal intLB As Integer, intUB As Integer) As Integer 'Int((upperbound - lowerbound + 1) * Rnd + lowerbound) GenRnd = Int((intUB - intLB + 1) * Rnd + intLB) End Function
VB Code:
Public Function Rand(ByVal low As Long, _ ByVal high As Long) As Long Rand = Int((high - low + 1) * Rnd) + low End Function Private Sub Form_Load()'forgot this Randomize End Sub Private Sub Command1_Click() Dim i as Integer i = rand(1000, 2000) End Sub