Generate two series of random numbers with condition

Viewed 69

How to generate 1000 sets consisting of 5 random numbers each (buy and sell prices) with the following requirements:

Prices do not have dependencies between sets only within a set. Prices should be positive numbers with 2 decimal places. E.g. drawn from a normal distribution with mean 40 and sd 4

Example

set_id 1

buy_prices: 23, 22, 21, 20, 19 -> buy prices should be sorted in desc order 
sell_prices: 24, 25, 26, 27, 28 -> sell prices should be sorted in ascending order 

Conditions:
buy prices: 1st > 2nd > 3rd > 4th > 5th
sell prices: 1st < 2nd < 3rd < 4th < 5th
In each set the first buy price should be < first sell_price
1 Answers

Maybe you can try the code as follows:

BuySell <- function(m = 40, s = 4) {
    S <- pmax(round(rnorm(10, m, s),2), 0)
    S.sorted <- sort(S)
    data.frame(buy = rev(head(S.sorted, 5)),sell = tail(S.sorted, 5))
}

lst <- replicate(1000,BuySell(),simplify = FALSE)

which gives output like below

> head(lst)
[[1]]
    buy  sell
1 38.94 39.12
2 38.93 40.36
3 38.72 42.03
4 38.54 42.58
5 34.86 43.07

[[2]]
    buy  sell
1 42.00 42.20
2 40.01 43.07
3 38.38 44.02
4 36.44 45.56
5 35.23 46.63

[[3]]
    buy  sell
1 41.12 41.26
2 41.01 41.44
3 40.70 41.74
4 36.28 43.04
5 34.97 44.33

[[4]]
    buy  sell
1 39.40 40.39
2 36.78 40.79
3 36.69 42.83
4 35.76 43.02
5 34.57 46.47

[[5]]
    buy  sell
1 38.60 39.03
2 37.90 40.65
3 37.13 43.17
4 35.36 43.74
5 29.99 47.76

[[6]]
    buy  sell
1 39.04 39.29
2 38.41 39.37
3 38.38 41.01
4 34.72 41.55
5 32.92 45.36
Related