Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomize between a min and max value

Status
Not open for further replies.

gripper111

IS-IT--Management
Jun 28, 2011
15
US
I created a VB form frmSettings there are two textboxes call txtTimerMin and txtTimerMax

I am trying to create a piece of code that will randomly choose a value between the min and max and save that to a variable "nextcycle"

I was wondering if somebody could give me some direction on this small block. Once I have the basics I will be able to extrapolate into other areas of my software

Thanks
 
Structure NextCycle
dim min as byte
dim max as byte
end structure

Public temp_min, temp_max as byte
Private Function GetRandomNumber(ByVal vnMinimumNumber As Integer, ByVal vnMaximumNumber As Integer)
Randomize() 'This sets a random seed to use with the Rnd function below.

'The Rnd Function returns a random number less than 1 but greater than or equal to 0

'The following line of code returns a random value between the
'vnMinimumNumber and mnMaximum number values passed into this method
GetRandomNumber = CInt(Int((vnMaximumNumber - vnMinimumNumber + 1) * Rnd() + vnMinimumNumber))

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
temp_min = val (txtTimerMin)
temp_max = val (txtTimerMax)
NextCycle.min =GetRandomNumber(temp_min, temp_max)
End Sub

You also have to create a sub that checks the data that user inserts in the textboxes so that to prevent characters like "ABCD....." "abcd......" "~!@#$%^....." because you will get error. Hope it helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top