创建随机数小于最大给定值 [英] create a random number less than a max given value

查看:139
本文介绍了创建随机数小于最大给定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会喜欢做的事是创建一个函数,该函数的参数是哪个号码的随机生成应该创造的极限。我曾经历过的一些发电机,只是重复生成的数了一遍又一遍。

我怎样才能让一台发电机不连续返回相同的号码。是否有人可以帮助我实现我的目标?

  INT randomGen(INT最大值)
{
  INT N;
  返回N;
}


解决方案

最简单的方法,从兰特得到均匀分布的结果是这样的:

  INT limited_rand(INT限制)
{
  INT R,D = RAND_MAX /限制;
  限制* = D;
  做{R = RAND(); }而(R> =限制);
  返回R / D;
}

其结果将是在 0 涨停,1 的范围内,而且每次都会以相同的概率为发生只要值 0 RAND_MAX 都与原来的兰特概率相等功能。

其他方法,如模运算或没有我以前介绍的偏置循环分裂。方法:通过浮点中间走不回避这个问题。获得良好的随机浮点数从兰特至少是一样困难。用我的整数函数(或它的改进版本)是开始,如果你想随机彩车的好地方。

修改:这是什么,我的意思是偏见的解释。假设 RAND_MAX 7和限制 5。假设(如果这是一个很好的兰特功能),该输出0,1,2,...,7都同样容易。服用兰特()%5 将映射0,1,2,3和4本身,而是图5,6,和7 0,1和2。这意味着,值0,1,和2的两倍,可能弹出的值3,如果你试图重新调整和划分,例如使用 RAND()*(4类似的现象发生双)限制/(RAND_MAX + 1)的这里,0和1映射到0,2和3映射到1,4映射到2,5和6映射至3,和7映射到4。

这些影响有些由 RAND_MAX 的幅度减轻,但他们可以回来,如果限制大。顺便说一句,正如其他人所说,线性同余的PRNG(典型实施兰特)的低位往往表现得非常糟糕,因此使用时模运算限制是2的功率可避免我所描述的偏差问题(因为限制通常把 RAND_MAX + 1 均匀地涂抹在这种情况下),但你碰上在其位置不同的问题。

What i would love to do is to create a function that takes a parameter that is the limit of which number the random generation should create. I have experienced that some generators that just repeat the number generated over and over again.

How can I make a generator that doesn't return the same number consecutively. Can someone please help me to achieve my goal?

int randomGen(int max)
{
  int n;      
  return n;
}

解决方案

The simplest way to get uniformly distributed results from rand is something like this:

int limited_rand(int limit)
{
  int r, d = RAND_MAX / limit;
  limit *= d;
  do { r = rand(); } while (r >= limit);
  return r / d;
}

The result will be in the range 0 to limit-1, and each will occur with equal probability as long as the values 0 through RAND_MAX all had equal probability with the original rand function.

Other methods such as modular arithmetic or dividing without the loop I used introduce bias. Methods that go through floating point intermediates do not avoid this problem. Getting good random floating point numbers from rand is at least as difficult. Using my function for integers (or an improvement of it) is a good place to start if you want random floats.

Edit: Here's an explanation of what I mean by bias. Suppose RAND_MAX is 7 and limit is 5. Suppose (if this is a good rand function) that the outputs 0, 1, 2, ..., 7 are all equally likely. Taking rand()%5 would map 0, 1, 2, 3, and 4 to themselves, but map 5, 6, and 7 to 0, 1, and 2. This means the values 0, 1, and 2 are twice as likely to pop up as the values 3 and 4. A similar phenomenon happens if you try to rescale and divide, for instance using rand()*(double)limit/(RAND_MAX+1) Here, 0 and 1 map to 0, 2 and 3 map to 1, 4 maps to 2, 5 and 6 map to 3, and 7 maps to 4.

These effects are somewhat mitigated by the magnitude of RAND_MAX, but they can come back if limit is large. By the way, as others have said, with linear congruence PRNGs (the typical implementation of rand), the low bits tend to behave very badly, so using modular arithmetic when limit is a power of 2 may avoid the bias problem I described (since limit usually divides RAND_MAX+1 evenly in this case), but you run into a different problem in its place.

这篇关于创建随机数小于最大给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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