【问题标题】:Generate two series of random numbers with condition生成两个有条件的随机数序列
【发布时间】:2020-07-13 07:30:11
【问题描述】:

如何生成 1000 个集合,每个集合包含 5 个随机数(买入和卖出价格),满足以下要求:

价格在集合之间没有依赖性仅在一个集合内。 价格应为带 2 位小数的正数。 例如。取自均值为 40 和 sd 4 的正态分布

例子

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

【问题讨论】:

    标签: r random


    【解决方案1】:

    或许你可以试试下面的代码:

    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)
    

    给出如下输出

    > 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      相关资源
      最近更新 更多