I'm working on my graduation project and within it i need a function gives me random ID numbers and random dates since 2008,01,01 untill now .. I'm done with the ID numbers but i couldnt find a solution for the dates .. how can I do this ????

Recommended Answers

All 2 Replies

What came to my mind when I see your question is to generate day number (1, 31) - Month (1,12) - year* (1900, 2009)
Say this method GenerateDate
Write method to CheckIsValidDate(day, month, year)
because it may you have 31-2-2008 which is wrong date. if CheckIsValidDate returns false call GenerateDate again untill CheckIsValidDate returns true

*Any mini-max number set it as you like

Here's a one way to do it

Dim StartDate As Date
Dim DaysUntilToday As Integer
Dim aRandomNum As Integer
Dim oRandom As System.Random
Dim aRandomDate As Date

' Start date (in my locale format)
StartDate = CDate("1.1.2008")
' Number of days until today
DaysUntilToday = System.DateTime.Now.Subtract(StartDate).Days
' Create a 'randomizer'
oRandom = New System.Random
' Get a random number between 0 - DaysUntilToday. Check that DaysUntilToday>=0
If DaysUntilToday >= 0 Then
  aRandomNum = oRandom.Next(DaysUntilToday)
Else
  aRandomNum = 0
End If
' Finally get a random date by adding a random number of days to start date
aRandomDate = StartDate.AddDays(aRandomNum)
' Debug: display the date
MessageBox.Show(aRandomDate.ToString, "Random Date", MessageBoxButtons.OK, MessageBoxIcon.Information)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.