【问题标题】:Get random number with focus on limitations (parabola curve)获取关注限制的随机数(抛物线)
【发布时间】:2019-02-28 02:42:21
【问题描述】:

我的数学知识可能仅限于自己解决这个问题。不过,我发现了一个类似的question,它关注的是中心而不是边框​​。

我想创建具有最大值和最小值的随机数,但重点是最大值和最小值,因此接近最大值和最小值的数字应该比介于两者之间的数字更频繁。

我记得在数学中有一个函数可以创建抛物线:

使用这个online tool,我生成了我正在寻找的函数,应该是这样的

y = 0.05 * x^2

这是我的基本想法并尝试将其转换为 javascript 我最终得到了这个:

for (let i = 0; i < 100; i++) {
  // Create random x with ^ 2 with max=100 and min=0
  let x = Math.pow(Math.floor(Math.random() * (100 - 0 + 1)) + 0, 2);
  // log the random number in the console
  console.log(0.05 * x);
}

但这当然没有任何实际意义,因为我也可以在控制台中看到(我订购了它们以便更清楚地看到)

> 0 0
> 0.05
> 0.05
> 0.2
> 0.2
> 0.8
> 2.45
> 3.2
> 3.2
> 3.2
> 4.05
> 4.05
> 4.05
> 6.05
> 8.45
> 8.45
> 14.45
> 16.2
> 16.2
> 16.2 20 20
> 22.05
> 22.05
> 22.05
> 26.45
> 28.8
> 28.8
> 31.25
> 33.8
> 36.45
> 39.2
> 42.05
> 48.05
> 48.05
> 48.05
> 51.2
> 57.8
> 57.8
> 57.8
> 64.8
> 64.8
> 64.8
> 68.45
> 76.05
> 88.2
> 88.2
> 92.45
> 92.45
> 92.45
> 96.8
> 101.25
> 105.8
> 110.45 125
> 130.05
> 130.05
> 135.2
> 140.45
> 140.45
> 145.8
> 145.8
> 145.8
> 156.8
> 162.45
> 162.45
> 162.45 180 180
> 198.45
> 204.8
> 204.8
> 224.45
> 231.2
> 231.2
> 231.2
> 231.2
> 238.05
> 238.05
> 259.2 320 320
> 328.05
> 344.45
> 352.8
> 352.8
> 361.25
> 369.8
> 387.2
> 387.2
> 423.2
> 423.2
> 432.45
> 451.25
> 460.8
> 470.45
> 470.45
> 490.05
> 490.05

谁可能有想法?

【问题讨论】:

    标签: javascript math random max min


    【解决方案1】:

    要生成具有所需分布的随机数,可以使用 Smirnov 变换 (inverse sampling)。

    要获得抛物线分布,只需对范围 0..1 使用均匀随机生成器并应用平方根。示例应生成 0..100 范围内的值,且两端的密度更高。

    for (let i = 0; i < 20; i++) {
      let x = Math.floor(50 * (Math.pow(Math.Random(), 0.5));
      if (Math.Random() < 0.5) 
          x = 50 - x
      else
          x = 50 + x;
       ...
    }
    

    【讨论】:

    • 不错的方法,感谢 Smirnov 的转换。它越来越接近解决方案,但是,我只寻找正数,而且,它只有一个限制输入,然后根据限制输入反转它。不过,感谢您采用这种很酷的方法!
    • 我添加了范围偏移,以获得 0..100 范围,最小值为 50
    • 谢谢,如果可以的话,我对您的回答做了一些更改。顺便说一句,如果重点应该更多地放在一端,您将如何进行,例如最低限度?
    【解决方案2】:

    Eventough @MBo 拒绝了我的编辑(但有正确答案),我仍将分享一个基于最小和最大变量(仅正数)的工作示例

    const max = 200;
    for (let i = 0; i < 20; i++) {
    
    const min = 40;
    const focus = 0.4; // 0 - 1, the lower the more focus on limitations
    
    let numbs = [];
    
    //The core function
    function randomFocusBorders(max, min) {
      const middle = (max - min) / 2;
      let x = Math.floor(50 * (Math.pow(Math.Random(), 0.5));
    
      let x = Math.floor(middle * Math.pow(Math.random(), focus));
      if (Math.Random() < 0.5) 
    
      if (Math.random() < 0.5) x = middle - x;
          x = 50 - x
    
      else x = middle + x;
      x += min;
      return x;
    
    }
      else
    
    
    
    //Call it 100 times and add value to numbs array
          x = 50 + x;
    
    for (let i = 0; i < 100; i++) {
      numbs.push(randomFocusBorders(max, min));
    
    }
    
    
    
    //Sort numbs array ascending
       ...
    
    numbs.sort((a, b) => {
      return a > b ? 1 : -1;
    }
    
    });
    
    
    console.log(numbs);
    

    工作jsFiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 2014-08-31
      • 2021-09-16
      • 2015-02-04
      • 2015-09-29
      • 1970-01-01
      相关资源
      最近更新 更多