Create random list of timestamps in python

Is there a way to create a list of random irregular timestamps in a strictly ascending format in Python? For instance:

20/09/2013 13:00        
20/09/2013 13:01        
20/09/2013 13:05        
20/09/2013 13:09        
20/09/2013 13:16        
20/09/2013 13:26   
+4
source share
1 answer

You can create a random generator.

  • You can generate randrange(60)a radiometer number between 0-60 (minutes)

  • use timedeltato add time to the actual date, in your case it is20/09/2013 13:..

  • Build a generator random_datewith a start date and the number of dates you want to create.

from random import randrange
import datetime 


def random_date(start,l):
   current = start
   while l >= 0:
      curr = current + datetime.timedelta(minutes=randrange(60))
      yield curr
      l-=1



startDate = datetime.datetime(2013, 9, 20,13,00)

for x in random_date(startDate,10):
  print x.strftime("%d/%m/%y %H:%M")

Output:

20/09/13 13:12
20/09/13 13:02
20/09/13 13:50
20/09/13 13:13
20/09/13 13:56
20/09/13 13:40
20/09/13 13:10
20/09/13 13:35
20/09/13 13:37
20/09/13 13:45
20/09/13 13:27

UPDATE

, , , . , , .

.

from random import randrange
import datetime 


def random_date(start,l):
   current = start
   while l >= 0:
    current = current + datetime.timedelta(minutes=randrange(10))
    yield current
    l-=1



startDate = datetime.datetime(2013, 9, 20,13,00)


for x in reversed(list(random_date(startDate,10))):
    print x.strftime("%d/%m/%y %H:%M")

:

20/09/13 13:45
20/09/13 13:36
20/09/13 13:29
20/09/13 13:25
20/09/13 13:20
20/09/13 13:19
20/09/13 13:16
20/09/13 13:16
20/09/13 13:07
20/09/13 13:03
20/09/13 13:01
+5

All Articles