Random time (1 Viewer)

joshirohany

Registered User.
Local time
Yesterday, 17:02
Joined
Apr 3, 2019
Messages
33
Dear Experts,
I am trying to generate Random time between two time range format hh:mm:ss. i am able to do in excel can you guide how it can be done in Ms Access.


Thanks and your help is appreciated.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:02
Joined
Oct 29, 2018
Messages
21,494
Hi. You should be able to use the Rnd() function for that. What have you tried?
 

joshirohany

Registered User.
Local time
Yesterday, 17:02
Joined
Apr 3, 2019
Messages
33
I am able to generate Random in number(integer) but not in date. i am using

Int ((200 - 150 + 1) * Rnd + 150)
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:02
Joined
Oct 29, 2018
Messages
21,494
I am able to generate Random in number(integer) but not in date. i am using

Int ((200 - 150 + 1) * Rnd + 150)
Okay, that's good. Now, you just need to convert it to a date. Try using the DateAdd() function for that.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:02
Joined
Feb 28, 2001
Messages
27,223
Can you tell us just a little bit more of what you wanted to do? Obviously you are looking to create a random time for some purpose, but the units might be confusing to you if you don't understand them. So ... are we talking a random number of days? A random number of hours? Something else? What are you trying to accomplish?
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:02
Joined
May 21, 2018
Messages
8,554
Code:
Public Function MyRandomTime(ByVal StartRange As Date, ByVal EndRange As Date, Optional Seed As Variant = 1) As Date
  'You have to seed it with a unique value per record if used in a query
  'To get a random long between two integers/longs
  Randomize
   Dim Range As Date
    Range = EndRange - StartRange
  MyRandomTime = CDate(CDbl(StartRange) + CDbl(Range) * Rnd(Seed))
End Function

Public Sub RandomTime()
  Dim I As Integer
  For I = 1 To 10
   Debug.Print MyRandomTime(#3/5/2020 10:00:00 AM#, #3/5/2020 10:30:00 AM#, 1)
  Next I
End Sub
Results
Code:
3/5/2020 10:28:53 AM
3/5/2020 10:05:37 AM
3/5/2020 10:07:27 AM
3/5/2020 10:15:59 AM
3/5/2020 10:22:45 AM
3/5/2020 10:23:36 AM
3/5/2020 10:29:02 AM
3/5/2020 10:04:44 AM
3/5/2020 10:01:44 AM
3/5/2020 10:18:43 AM
 
Last edited:

joshirohany

Registered User.
Local time
Yesterday, 17:02
Joined
Apr 3, 2019
Messages
33
Firstly thanks to all of you.

All i am trying, is to generate In/Out time for employees. I have attached the excel sheet that i am currently using. Need the same in Access.
 

Attachments

  • Random.zip
    8.7 KB · Views: 83

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:02
Joined
May 21, 2018
Messages
8,554
And the answer previously provided?
 

ebs17

Well-known member
Local time
Today, 02:02
Joined
Feb 7, 2020
Messages
1,950
I am able to generate Random in number(integer)
You can convert a time to seconds (=> long):
Code:
?DateDiff("s", #0:00:00#, #11:02:15#)
 39735

That would be done for the area boundaries. The random number that can now be determined can be calculated back in time:
Code:
?DateAdd("s", 39999, #0:00:00#)
11:06:39
 

joshirohany

Registered User.
Local time
Yesterday, 17:02
Joined
Apr 3, 2019
Messages
33
Hello MajP,
I downloaded your demo. Its showing below details.
1583507922942.png


1583507922942.png
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:02
Joined
May 21, 2018
Messages
8,554
Not the right file. Not sure what that is. Standby.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:02
Joined
May 21, 2018
Messages
8,554
Try this file instead
 

Attachments

  • RandomWorkTime.zip
    43.1 KB · Views: 86

joshirohany

Registered User.
Local time
Yesterday, 17:02
Joined
Apr 3, 2019
Messages
33
Its exactly what is required. Thanks sir and all once again.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 20:02
Joined
May 21, 2018
Messages
8,554
Fyi,
I noticed that it does not include end time values. So in the AM you get values equally distributed from 7:45 to 7:59, but not 8:00. You can modify the code or just extend the ranges by 1 minute.
MorningEndRange = Pdate + Me.MorningEndRange - Int(Me.MorningEndRange) + 1 / 1440
EveningEndRange = Pdate + Me.EveningEndRange - Int(Me.EveningEndRange) + 1 / 1440

You can modify the code as follows to include 800 and 1500.
 

Users who are viewing this thread

Top Bottom