Generate random times

BC2005

New Member
Joined
Dec 7, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone,
Wondering if anyone can help me out?
Basically I want to create a random time generator for times that fall between a start and end time when the end time is the following day, I would also like to exclude certain time frames.
An example would be : 8 random times needed to be generated between 17:30 and 01:30(inclusive) but need to exclude times from 19:45-20:00, 22:00-22:30, 00:45-01:00.

I have searched and searched for something but cannot seem to find anything that will work.

Any help would be greatly appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
VBA Code:
Sub Random_Times()
' 8 random times needed to be generated between 17:30 and 01:30(inclusive)
'but need to exclude times from 19:45-20:00, 22:00-22:30, 00:45-01:00.
   
'random to the nearest minute
   
    Static IsRandomized As Boolean
    Dim RandTime(1 To 8) As String, rt As Date
   
    If Not IsRandomized Then Randomize: IsRandomized = True    'Seed randomizer once
   
    For i = 1 To 8
        Do
            DoEvents
            rt = Int((480 - 0 + 1) * Rnd + 0) / 1440 'random 0-480 minutes (time span in minutes)
            rt = rt + 1050 / 1440                    'shift random minute to between 17:30 and 01:30
            rt = rt - IIf(rt >= 1, 1, 0)             'subtract day if past 2400
            RandTime(i) = Format(rt, "hh:mm")        'Format time as text for msgbox
            Select Case rt                           'Exclude time ranges
                Case TimeValue("19:45") To TimeValue("20:00")
                Case TimeValue("22:00") To TimeValue("22:30")
                Case TimeValue("00:45") To TimeValue("01:00")
                Case Else: Exit Do
            End Select
        Loop
    Next
   
    MsgBox Join(RandTime, vbLf), , "Eight Random Times"
   
End Sub
 
Last edited:
Upvote 0
Here is the strategy I took.

Create a table based on the allowable time intervals you gave.
Determine the duration of each allowable interval. (The total allowable time is 7 hours.)
Create a parallel table that covers 00:00 to 7:00 using the same increments as your intervals.
Generate a random time. Look it up in the parallel table. On that row will be the corresponding allowable interval.
Calculate the time from the beginning of the parallel interval, and then add it to the beginning of the allowable interval to calculate the actual time.

You have to have one cell with the random number and another cell with the formula, because the random number is used in three places in the formula. If you use RAND In the formula you will get three different random numbers.

Here is what it looks like. Here is a link.


BC2005=random times.xlsx
ABCDEFGHIJ
1Random TimeAdjusted TimeDurationSpanStartEndDay offsetEnd
20:1817:482:150:002:1517:3019:45019:45
32:002:154:1520:0022:00022:00
42:154:156:3022:300:4510:45
50:306:307:001:001:3011:30
Sheet2
Cell Formulas
RangeFormula
A2A2=RAND()*$F$5
B2B2=A2-VLOOKUP(A2,$E$2:$G$5,1,TRUE)+VLOOKUP(A2,$E$2:$G$5,3,TRUE)
D2:D5D2=MOD(J2-G2,1)
F2:F5F2=E2+D2
E3:E5E3=F2
J2:J5J2=H2+I2
 
Upvote 0
10,000 random times - please see image:
Generate_random_times.jpg

Copy A5:C5 down to row 10,001:
Cell Formulas
RangeFormula
A2:A5A2=RAND()*TIME(7,0,0)
B2:B5B2=MOD(A2+VLOOKUP(A2,$D$2:$E$5,2,TRUE),1)
C2:C5C2=RAND()
E2E2=TIME(17,30,0)
D3D3=TIME(19,45,0)-TIME(17,30,0)
E3E3=E2+TIME(20,0,0)-TIME(19,45,0)
D4D4=D3+TIME(22,0,0)-TIME(20,0,0)
E4E4=E3+TIME(22,30,0)-TIME(22,0,0)
D5D5=D4+TIME(23,59,0)+ TIME(0,46,0)-TIME(22,30,0)
E5E5=E4+TIME(1,0,0)-TIME(0,45,0)
 
Upvote 0
Here is the strategy I took.

Create a table based on the allowable time intervals you gave.
Determine the duration of each allowable interval. (The total allowable time is 7 hours.)
Create a parallel table that covers 00:00 to 7:00 using the same increments as your intervals.
Generate a random time. Look it up in the parallel table. On that row will be the corresponding allowable interval.
Calculate the time from the beginning of the parallel interval, and then add it to the beginning of the allowable interval to calculate the actual time.

You have to have one cell with the random number and another cell with the formula, because the random number is used in three places in the formula. If you use RAND In the formula you will get three different random numbers.

Here is what it looks like. Here is a link.


BC2005=random times.xlsx
ABCDEFGHIJ
1Random TimeAdjusted TimeDurationSpanStartEndDay offsetEnd
20:1817:482:150:002:1517:3019:45019:45
32:002:154:1520:0022:00022:00
42:154:156:3022:300:4510:45
50:306:307:001:001:3011:30
Sheet2
Cell Formulas
RangeFormula
A2A2=RAND()*$F$5
B2B2=A2-VLOOKUP(A2,$E$2:$G$5,1,TRUE)+VLOOKUP(A2,$E$2:$G$5,3,TRUE)
D2:D5D2=MOD(J2-G2,1)
F2:F5F2=E2+D2
E3:E5E3=F2
J2:J5J2=H2+I2
Hello,

If I am not mistaken, your approach does not generate any time between 0:00 and 1:00:
MrExcel_Generate random times.xlsm
ABCDEFGHIJ
1Random TimeTime6StringJazzerDurationSpanStartEndDay offsetEnd
204:35:0922:50:090,5869778102:1500:0002:1517:3019:45019:45
305:17:3923:32:390,0316378902:0002:1504:1520:0022:00022:00
405:22:4423:37:440,8767466402:1504:1506:3022:3000:45100:45
504:44:4322:59:430,5775258900:3006:3007:0001:0001:30101:30
6StringJazzer
Cell Formulas
RangeFormula
A2:A5A2=RAND()*$F$5
B2:B5B2=A2-VLOOKUP(A2,$E$2:$G$5,1,TRUE)+VLOOKUP(A2,$E$2:$G$5,3,TRUE)
C2:C5C2=RAND()
D2:D5D2=MOD(J2-G2,1)
F2:F5F2=E2+D2
E3:E5E3=E2+D2
J2:J5J2=H2+I2


Generate_random_times_6StringJazzer.jpg
 
Upvote 0

Forum statistics

Threads
1,214,854
Messages
6,121,941
Members
449,056
Latest member
denissimo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top