How to generate irregular time intervals?

659 views Asked by At

I intend to make a simulation of real-data ie produce data at irregular time intervals. Below is some prototype code I wrote in python to simulate irregular time(not data), but the results are that my loop runs too fast such that each "time"(dat) produced is produced at the same time stamp. On top of this code, I intend to feed data set and pass in the data at these irregular time stamps.

import time,random

Tadd=0.1
start=time.time()
while time.time()<(start+Tadd):
     x=random.uniform(0,1)
     if x<0.5:
         dat=time.time()
         print dat
     else:
         pass

The output is something was this.

1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95

So my question is:

  1. Is it viable or "realistic" for me to code up this way? Or am I better off reading a text file line by line. eg read the 1st line process, read the 2nd line process, .... My intention is actually to read sensor data for robot localisation. But, which is the best simulation. I was thinking that perhaps I can use an event to produce data from my mouse. Can someone provide insight and suggestions on how I can go about this? Or am I better off just diving straight into the practical setting from a simulated setting and buy an ultrasound sensor.

  2. Or if I am on the right track, and this is a good enough simulation. How do I make my time intervals ,in this case, less sensitive so that each "time"(dat) is distinct from the other.

2

There are 2 answers

0
Mangat Rai Modi On

You simply produce a random number between and add it to the current time. This is assuming you don't need time in the past only.

Edit: Now the code will generate timestamps in increasing order, separated by an average of one second.

import time,random

Tadd=0.1
start=time.time()
init = 0
while time.time()<(start+Tadd):
     x=random.uniform(init,init+1)
     dat=time.time()+x
     init+=1;
     print dat

print start, start+Tadd
0
101 On

It shouldn't be hard to simulate data arriving at irregular intervals. However, you aren't adding in any delays between data points (apart from code execution time), so you probably want to explicitly add them. For example this code will run for 5 seconds and print the time at random intervals. It shouldn't be hard to tweak this to match your data source.

import time
import random

run_time = 5
start = time.time()
now = time.time()
while now < start + run_time:
    print now
    time.sleep(random.random())
    now = time.time()