Generate random date ranges where range size is decided from discrete distribution

user3139545

I have the following function to generate a random date between two dates:

val random = new Random(System.nanoTime)
def randomStartTime(days: Integer): LocalDateTime = {
  // Uses to=today and randomly selects a from day maximum days back in time
  val to = LocalDateTime.now()  
  val from = to.minusDays(days) 
  val diff = SECONDS.between(from, to)     
  from.plusSeconds(random.nextInt(diff.toInt))
}

Now I try to write a function that returns a start and end date with a diff between start and stop of size x. I want x to be selected from a discrete distribution that I can specify, for example:

singleDay = .4
week = .1
month = .1
twoMonths = .1
year = .1
3Years = .1
over3Years = .1

This says that there is a 40% chance of a date range diffing with 1day.

ALSO

the signature of the function should be dateRange(endDate: LocalDate, daysBack: Integer)

For example I send in dateRange("2016-04-03", 600), now return a daterange of size x between 2016-04-03 - 600days AND 2016-04-03 This adds the problem of some ranges are not possible to produce at all if the range is to narrow, for example the 3year range in this example.

The question I have is how this should be done in Scala, the problem im having is how to construct a descrite distribution in an idiomatic way and how to constrain the range based on the allowed input range.

Yony Yatsun

Try this

def dateRange(date : String, daysBack : Int) : Int =
{
    val listAvailable = List(1,7,daysInMonth(date), daysInTwoMonth(date), 365, 1095, 1095 + Math.abs(Random.nextInt)).filter(_ < daysBack)
    val listDistribution = List(40.0,10.0,10.0,10.0,10.0,10.0,10.0).take(listAvailable.size)
    val maxDistribution = listDistribution.sum
    val listDistributionUpdated = listDistribution.map{distribution => (distribution * 100) / maxDistribution}
    val value = Random.nextInt(100).toDouble
    var index = 0
    var found = false
    var foundIndex = 0
    var sumValue = 0.0
    while(index < listDistribution.size && !found)
    {
        sumValue = sumValue + listDistribution(index)
        if(sumValue >= value)
        {
            found = true
            foundIndex = index 
        }
        index = index + 1
    }

    listAvailable(foundIndex)
}

def daysInMonth(date String): Int =
{
 //implement according to date 30 or 31
}

def daysInTwoMonth(date String): Int =
{
 //implement
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Generate Random Number in Range from Single-Tailed Distribution with Python

Choosing a random value from a discrete distribution

How do I generate discrete random events with a Poisson distribution?

std::discrete_distribution of a specified range of random numbers

Generate random bytearray from a distribution

Generate random integer from discrete selection?

How to generate a random date from a given range with hive

Python generate range array with random step size

Generate random number from custom distribution

Generate random numbers from lognormal distribution in python

Generate a random number from normal distribution in J

Generate random variables from a probability distribution

Generate random numbers from a normal distribution

Random distribution like discrete_distribution<float>

Group based on discrete date ranges

Generate years of date ranges from a set of dates

Generate random numbers between range using std::binomial_distribution

C++ - generate random numbers following normal distribution within range

Generate random numbers in a specific range using beta distribution

Java: Generate a random date between a range (range starting from current date/time to a random future date (e.g 5-10 days from current date/time)

Generate random ranges

Generate date range from two date columns

How to generate random numbers in range from INPUT?

How to generate a random number in a list from a range

Delphi generate random date time value between range of date time

Check if date between date range in excel where there are multiple date ranges in a list and output corresponding range label

Generate Date ranges in JodaTime

Find common date range from a set of overlapping date ranges

Sampling from a discrete distribution in Julia