如何生成的范围内偏于值随机数? [英] How to generate random numbers biased towards one value in a range?

查看:277
本文介绍了如何生成的范围内偏于值随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说,如果我想生成的一个不带偏见的随机数分最高,我会怎么做:

  VAR兰特=功能(最小值,最大值){
    返回Math.floor(的Math.random()*(最大 - 最小+ 1))+分钟;
};
 

但是,如果我要生成的随机数分钟最高,但对一个价值比较偏 N 最高到一定程度 D ?这是最好的一个概率曲线说明吧:

解决方案

下面是一个方法:

  • 获取最小 - 最大范围内的随机数
  • 在得到一个随机标准化的混合值
  • 在混合随机带偏见的基础上随机搭配

也就是说,在伪:

变量:
  分= 0
  最大= 100
  偏向= 67(N)
  影响力= 1(D)[0.0,1.0]

公式:
  RND =随机()×(最大值 - 最小值)+分
  混合=随机()x影响
   = RND×(1  - 混合)+偏差x组合

该混合系数可以降低一个次要因素来设定它应该多少影响(即混合*系数其中的因素是[0,1])。

演示

这将绘制偏见的随机范围。上带有1个为影响力,底部0.75的影响。偏压这里设定为在范围2/3位置。 底部带是没有(刻意)偏移比较。

VAR CTX = document.querySelector(画布)。的getContext(2D ); ctx.fillStyle =红色; ctx.fillRect(399,0,2,110); //绘制偏见目标 ctx.fillStyle =RGBA(0,0,0,0.07); 功能getRndBias(最小值,最大值,偏差,影响力){     VAR RND =的Math.random()*(最大值 - 最小值)+分钟,//随机在范围内         混合=的Math.random()*的影响; //随机搅拌机     返回RND *(1 - 混合)+偏置*混合; //混合品种齐全,偏置 } //情节偏向结果 (函数循环(){   为(变种I = 0; I&小于5;我++){//刚刚的子帧(更快情节)     ctx.fillRect(getRndBias(0,600,400,1.00),4,2,50);     ctx.fillRect(getRndBias(0,600,400,0.75),55,2,50);     ctx.fillRect(的Math.random()* 600,115,2,35);   }   requestAnimationFrame(环); })();

<画布宽度= 600>< /帆布>

Say, if I wanted to generate an unbiased random number between min and max, I'd do:

var rand = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
};

But what if I want to generate a random number between min and max but more biased towards a value N between min and max to a degree D? It's best to illustrate it with a probability curve:

解决方案

Here is one way:

  • Get a random number in the min-max range
  • Get a random normalized mix value
  • Mix random with bias based on random mix

Ie., in pseudo:

Variables:
  min = 0
  max = 100
  bias = 67      (N)
  influence = 1  (D) [0.0, 1.0]

Formula:
  rnd = random() x (max - min) + min
  mix = random() x influence
  value = rnd x (1 - mix) + bias x mix

The mix factor can be reduced with a secondary factor to set how much it should influence (ie. mix * factor where factor is [0, 1]).

Demo

This will plot a biased random range. The upper band has 1 as influence, the bottom 0.75 influence. Bias is here set to be at 2/3 position in the range. The bottom band is without (deliberate) bias for comparison.

var ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "red"; ctx.fillRect(399,0,2,110);  // draw bias target
ctx.fillStyle = "rgba(0,0,0,0.07)";

function getRndBias(min, max, bias, influence) {
    var rnd = Math.random() * (max - min) + min,   // random in range
        mix = Math.random() * influence;           // random mixer
    return rnd * (1 - mix) + bias * mix;           // mix full range and bias
}

// plot biased result
(function loop() {
  for(var i = 0; i < 5; i++) {  // just sub-frames (speedier plot)
    ctx.fillRect( getRndBias(0, 600, 400, 1.00),  4, 2, 50);
    ctx.fillRect( getRndBias(0, 600, 400, 0.75), 55, 2, 50);
    ctx.fillRect( Math.random() * 600          ,115, 2, 35);
  }
  requestAnimationFrame(loop);
})();

<canvas width=600></canvas>

这篇关于如何生成的范围内偏于值随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆