生成指定范围内的随机数 - 各种情况(int,float,inclusive,exclusive) [英] Generate random numbers in specified range - various cases (int, float, inclusive, exclusive)

查看:420
本文介绍了生成指定范围内的随机数 - 各种情况(int,float,inclusive,exclusive)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出 Math.random()函数,该函数返回[0,1)和 min之间的数字 max 指定范围的值,我们如何为以下情况生成数字:

given a Math.random() function which returns a number between [0,1) and min max values to specify the range, how can we generate numbers for the following cases:

我们想要的案例整数


  • A :(最小,最大)?

  • B:[min,max]返回Math.floor(Math.random()*(max - min))+ min;

  • C :(最小,最大)?

  • D :[min,max]返回Math.floor(Math.random()*(max - min + 1))+ min;

  • A: (min,max) ?
  • B: [min,max) return Math.floor(Math.random() * (max - min)) + min;
  • C: (min,max] ?
  • D: [min,max] return Math.floor(Math.random() * (max - min + 1)) + min;

我们想要的案例浮动


  • A: (分钟,最大)?

  • B:[min,max]返回Math.random()*(max - min)+分钟;

  • C :(分钟,最大值)?

  • D:[min,max]?

  • A: (min,max) ?
  • B: [min,max) return Math.random() * (max - min) + min;
  • C: (min,max] ?
  • D: [min,max] ?

推荐答案

整数
你的B.公式是正确的,其他一切是通过微不足道的 +1 -1 更正:


  • A。 (min,max)= [min + 1,max),因此来自B.我们获得
    min + 1 + Math.floor (Math.random()*(max - min - 1))

  • B. min + Math.floor(Math.random()*(max - min))

  • C.由于在区间运算(min,max] = max - [0,max - min)中,还可以编写 max - Math.floor( Math.random()*(max - min))

  • D. [min,max] = [min,max + 1] ,因此: min + Math.floor(Math.random()*(max + 1分钟))

  • A. (min, max) = [min + 1, max), therefore from B. we obtain min + 1 + Math.floor(Math.random() * (max - min - 1))
  • B. min + Math.floor(Math.random() * (max - min))
  • C. Since in the interval arithmetic (min, max] = max - [0, max - min), one could also write max - Math.floor(Math.random() * (max - min))
  • D. [min, max] = [min, max + 1), therefore: min + Math.floor(Math.random() * (max + 1 - min))

浮动。正如V13已经指出的那样,问题有点不合理:如果我们将单点视为测量零集,那么几乎(在测量 - 理论意义上)四组之间没有差异......但是,如果你想保证排除的区间边界永远不会(不仅仅是几乎从不)采样,如果你认为没有舍入错误,你可以这样做:

Floats. As V13 already pointed out, the question is somewhat ill-posed: if we consider single points as measure-zero sets, there is almost (in measure-theoretical sense) no difference between the four sets... However, if you want to guarantee that the excluded interval boundaries are never (not merely "almost never") sampled, and if you assume that there are no rounding errors, you could do something like this:


  • A: var middle =(min + max)/ 2; var sign = Math.random()> 0.5? 1:-1;返回中间+符号*(最大 - 最小)/ 2 * Math.random(); 这个解决方案在 0 上放一点点质量但是,对于所有实际目的,这应该可以忽略不计。

  • A: var middle = (min + max) / 2; var sign = Math.random() > 0.5 ? 1 : -1; return middle + sign * (max - min) / 2 * Math.random(); This solution puts a tiny little bit more mass on 0, but this should be negligible for all practical purposes.

B: min + Math.random()*(max - min) ,是的。

A和D之间的区别是以下:如果我们尝试在A中使用公式 min + Math.random()*(max - min),我们可能偶尔会得到 0 (因为可能的数字范围实际上是有限的)。但是,没有合理的统计数据可能会抱怨D的上限没有被击中。

The difference between A and D is the following: if we tried to use the formula min + Math.random() * (max - min) in A, we might occasionally get a 0 (because the range of possible numbers is actually finite). However, no reasonable statistic could ever complain that the upper bound is not hit in D.

这篇关于生成指定范围内的随机数 - 各种情况(int,float,inclusive,exclusive)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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