How to generate random timestamps with N-seconds difference in Spark 2?
Tag : scala , By : user90210
Date : March 29 2020, 07:55 AM
hope this fix your issue I need to generate a new column in my DataFrame with random timestamps that would have a step of seconds. The DataFrame contains 10.000 rows. The starting timestamp should be 1516364153. I tried to solve the problem as follows: , Just use rand: import org.apache.spark.sql.functions.rand
df.withColumn("timestamp", (lit(1516364153) + rand() * 2000)).cast("long"))
|
Generate random list of timestamps in python
Date : March 29 2020, 07:55 AM
this one helps. The whole point of the datetime library: datetime, timedelta, etc. objects act as much like numbers as possible, so you can just do arithmetic on them. So, to generate a uniform distribution over a day, just take a uniform distribution from 0.0 to 1.0, and multiply it by a day:1 td = random.random() * datetime.timedelta(days=1)
dt = datetime.datetime(2018, 5, 1) + random.random() * datetime.timedelta(days=1)
dt = random.random() * (end - start) + start
[random.random() * (end - start) + start for _ in range(10)]
def randomtimes(stime, etime, n):
frmt = '%d-%m-%Y %H:%M:%S'
stime = datetime.datetime.strptime(start, frmt)
etime = datetime.datetime.strptime(end, frmt)
td = etime - stime
return [random.random() * td + stime for _ in range(n)]
|
Generate random list of timestamps within multiple time intervals in python
Tag : python , By : Harry Truman
Date : March 29 2020, 07:55 AM
Hope that helps Here is a way to do it: the idea is that if we remove the total duration of the periods from the time available, generate start times in the period that is left, and then postpone them with the cumulated periods before them, we are sure that the intervals won't overlap. from datetime import datetime, timedelta
import random
def generate_periods(start, end, durations):
durations = [timedelta(minutes=m) for m in durations]
total_duration = sum(durations, timedelta())
nb_periods = len(durations)
open_duration = (end - start) - total_duration
delays = sorted(timedelta(seconds=s)
for s in random.sample(range(0, int(open_duration.total_seconds())), nb_periods))
periods = []
periods_before = timedelta()
for delay, duration in zip(delays, durations):
periods.append((start + delay + periods_before,
start + delay + periods_before + duration))
periods_before += duration
return periods
durations = [32, 24, 4, 20, 40, 8, 27, 18, 3, 4]
start_time = datetime(2019, 9, 2, 9, 0, 0)
end_time = datetime(2019, 9, 2, 17, 0, 0)
generate_periods(start_time, end_time, durations)
# [(datetime.datetime(2019, 9, 2, 9, 16, 1),
# datetime.datetime(2019, 9, 2, 9, 48, 1)),
# (datetime.datetime(2019, 9, 2, 9, 58, 57),
# datetime.datetime(2019, 9, 2, 10, 22, 57)),
# (datetime.datetime(2019, 9, 2, 10, 56, 41),
# datetime.datetime(2019, 9, 2, 11, 0, 41)),
# (datetime.datetime(2019, 9, 2, 11, 2, 37),
# datetime.datetime(2019, 9, 2, 11, 22, 37)),
# (datetime.datetime(2019, 9, 2, 11, 48, 17),
# datetime.datetime(2019, 9, 2, 12, 28, 17)),
# (datetime.datetime(2019, 9, 2, 13, 4, 28),
# datetime.datetime(2019, 9, 2, 13, 12, 28)),
# (datetime.datetime(2019, 9, 2, 15, 13, 3),
# datetime.datetime(2019, 9, 2, 15, 40, 3)),
# (datetime.datetime(2019, 9, 2, 16, 6, 44),
# datetime.datetime(2019, 9, 2, 16, 24, 44)),
# (datetime.datetime(2019, 9, 2, 16, 37, 42),
# datetime.datetime(2019, 9, 2, 16, 40, 42)),
# (datetime.datetime(2019, 9, 2, 16, 42, 50),
# datetime.datetime(2019, 9, 2, 16, 46, 50))]
|
how to generate random unix timestamps mysql
Tag : mysql , By : antonio
Date : March 29 2020, 07:55 AM
|
Why does Excel generate my CSV in a random fashion (PHP)?
Date : March 29 2020, 07:55 AM
|