How to field-calculate random dates for a specific time period?

3096
14
Jump to solution
01-22-2018 09:01 PM
Highlighted
New Contributor II

How to field-calculate random dates for a specific time period using Python when calculating date field in ArcGIS Pro?

It is something I need to do when creating fictitious demo data.  For example, I would like to create random dates & time between 01/01/2017 00:00:00 AM and 12/31/2017 11:59:00 PM.

Please help if you have any idea! Thank you very much, guys!

Best regards,

Thao Nguyen

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Esteemed Contributor

To calculate a random date based on the link provided by Dan you could do something like this:

Code Block:

def randomDate(start, end):
    import random
    import time
    from datetime import datetime
    frmt = '%d-%m-%Y %H:%M'

    stime = time.mktime(time.strptime(start, frmt))
    etime = time.mktime(time.strptime(end, frmt))

    ptime = stime + random.random() * (etime - stime)
    dt = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
    return dt

Define the range for which the random dates should be generated. For example:

randomDate("20-01-2018 13:30", "23-01-2018 04:50")

Resulting dates will be in this range in the output date field:

View solution in original post

14 Replies
Highlighted
MVP Esteemed Contributor

There are a variety of ways to calculate random dates.

Have you checked out web solutions using python?  They could obviously be implemented in the field calculator.

For example

https://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates

http://www.daveoncode.com/2013/05/20/generate-random-dates-in-python-using-datetime-and-random-modul...

or are you looking for a builtin function?

Highlighted
New Contributor II

Actually, I've tried to calculate field following those solutions, but the result was not as I expected. The return should be date + timestamp such as 01/01/2017 00:00:00 AM. If you have any exact python example for my reference, it would be highly appreciated. Thank you very much.

Reply
0 Kudos
Highlighted
Esri Esteemed Contributor

To calculate a random date based on the link provided by Dan you could do something like this:

Code Block:

def randomDate(start, end):
    import random
    import time
    from datetime import datetime
    frmt = '%d-%m-%Y %H:%M'

    stime = time.mktime(time.strptime(start, frmt))
    etime = time.mktime(time.strptime(end, frmt))

    ptime = stime + random.random() * (etime - stime)
    dt = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
    return dt

Define the range for which the random dates should be generated. For example:

randomDate("20-01-2018 13:30", "23-01-2018 04:50")

Resulting dates will be in this range in the output date field:

View solution in original post

Highlighted
Esri Esteemed Contributor

As alternative to python you can also select Arcade as language and use this:

In this case the range of dates is adjusted to the one you asked for (with a slight change):

01/01/2017 00:00:00 AM and 12/31/2017 11:59:59 PM

Arcade expression:

var start_date = Date(1483246800000);
var end_date = Date(1514782799000);
var seconds = DateDiff(end_date, start_date, "seconds");
var rnd_sec = Random() * seconds;
var rnd_date = DateAdd(start_date, rnd_sec, "seconds");
return rnd_date;

This site ( Epoch Converter - Unix Timestamp Converter ) might help to determine the number of miliseconds since 1/1/1970 or you can just use this format Date ( value , month , day , hour , minute , second , millisecond )

Output dates will be like this:

And when you look at the result, you will notice that the result is not random for each record (which I would have expected) but random for each run of the field calculator.  

Kory Kramer‌ (sorry to bother you again), am I missing something in order to make the Random generate a random result for each record? The help doesn't say much: Math Functions | ArcGIS for Developers 

Highlighted
MVP Esteemed Contributor

Random, whether math or numpy, should  generate a random number unless the randomseed is somehow set

Reply
0 Kudos
Highlighted
Regular Contributor II

Sometime when working with the field calculator the random function seems to hold a static seed as if the function throught the calculation remains as a static variable...I have ran into problems using random numbers in the past within the field calculator so have moved that logic to the database in my shop .....

Resetting the seed.... would that help?  Python: Random System time seed - Stack Overflow 

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

xander_bakker‌, don't be tempted by the dark side/Arcade.   Arcade is, in many ways, classic Esri.  Why work within existing frameworks when you can create something unique and special that no one else will adopt!

Highlighted
Esri Esteemed Contributor

Actually, there are cool things you can do with Arcade (see for instance what I posted here: Create pop-ups in ArcGIS Online with conditional images using Arcade ) and I can use this in more places than I will be able to with Python. Arcade is still in development, but we will see some nice features being released in the near future which will require ArcGIS Arcade | ArcGIS for Developers . . 

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

"I can use this in more places than I will be able to with Python," [as long as all of those places are ArcGIS].  I doubt anyone else will adopt Arcade, and why should they when JavaScript, Python, and others already exist and are broadly adopted beyond a single platform.  Whatever the merits of Arcade are, creating a new expression language/syntax takes a very self-centered view of the geospatial and data world.

Reply
0 Kudos