Easy Random Number Function (Min, Max & Round)
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 Function
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
NOTE: Update on 7/29/05 switched max to the first parameter and Min to the second (and made it default optional).
Let me know if you like this module or used it in a cool way.
Re: Easy Random Number Function (Min, Max & Round)
A random timer:
VB Code:
'''''''''''''''''''Random Timer'''''''''''''''''''''''''''''''''''''''
'Credits: EyeRmonkey for the random module
'Created By: AliasXNeo
'Public use
Option Explicit
Private Sub Command1_Click()
If Command1.Caption = "Stop Timer" Then 'Checking if it's running already
'If it is, disable it
Timer1.Enabled = False
Timer2.Enabled = False
Command1.Caption = "Start Timer"
Else 'If it isn't:
Command1.Caption = "Stop Timer" 'Make sure they can stop it
'Randomize the time between
Label2.Caption = rand(1, 10)
Label4.Caption = rand(1, 10)
'Set the timer interval random from the two random results, and times by 1000 for seconds
Timer1.Interval = rand(Label2.Caption, Label4.Caption) * 1000
'Set the countdown timer, and format it
Label6.Caption = Timer1.Interval / 1000
'Enabled everything
Timer1.Enabled = True
End If
End Sub
Private Sub Timer1_Timer()
'Randomize to get the between
Label2.Caption = rand(1, 10)
Label4.Caption = rand(1, 10)
'Randomize our two results, and times by 1000 for seconds
Timer1.Interval = rand(Label2.Caption, Label4.Caption) * 1000
Label6.Caption = Timer1.Interval / 1000
End Sub
It's good for the kind of stuff I program :)
Re: Easy Random Number Function (Min, Max & Round)
what are you trying to do?
make a form backcolor to changed every time it opend?
.
Re: Easy Random Number Function (Min, Max & Round)
Yeah, I'm lost on that one. I can't believe I ever posted this. But while I'm here I will post an updated version that allows decimals.
Re: Easy Random Number Function (Min, Max & Round)
wow !! your code is way way to long
VB Code:
Private Sub Command1_Click()
Randomize
r = Int(255 * Rnd + 0)
g = Int(255 * Rnd + 0)
b = Int(255 * Rnd + 0)
Form1.BackColor = RGB(r, g, b)
End Sub
Private Sub Form_Load()
Randomize
r = Int(255 * Rnd + 0)
g = Int(255 * Rnd + 0)
b = Int(255 * Rnd + 0)
Form1.BackColor = RGB(r, g, b)
End Sub
Re: Easy Random Number Function (Min, Max & Round)
I think you are missing the point. It is suppose to be all purpose. As in I can put that module into anything and use it to get any kind of random number. If all I wanted to do was get 3 random numbers then that would be fine.
Also, you don't need + 0 after everything.
Re: Easy Random Number Function (Min, Max & Round)
Quote:
Originally Posted by eyeRmonkey
I think you are missing the point. It is suppose to be all purpose. As in I can put that module into anything and use it to get any kind of random number. If all I wanted to do was get 3 random numbers then that would be fine.
Also, you don't need + 0 after everything.
yes you do need 0 after every single rendom generated number
Re: Easy Random Number Function (Min, Max & Round)
My code was simply just making a completely random timer. I was frankly bored and was just thinking of something to make with it, and I came up with that. Make it yourself, as you can see it gets two random numbers, then get another random number betweeen those two random numbers giving a very smooth random. For the stuff I do, it's very handy.
Re: Easy Random Number Function (Min, Max & Round)
Quote:
Originally Posted by wiz126
yes you do need 0 after every single rendom generated number
If you actually think about it, it's completely pointless ;)
Re: Easy Random Number Function (Min, Max & Round)
well the way he did it you must have the 0 or it will b wrong