简体   繁体   English

使用rand()时输出范围内的随机数

[英]Output Random Number in range when using rand()

My Specific question is when i input a range like 90 as the lower value and 100 as the higher value i get output random number sometimes less than 90. In my code below , x<y. 我的具体问题是,当我输入一个范围,例如90作为较低的值而100作为较高的值时,我得到的输出随机数有时小于90。 In my code below , x<y. My code is: 我的代码是:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define Null 0

void main(){
    int x,y,a,b;
    printf("Insert two numbers to create a range\n in which you want a random number to be generated:\n");
    scanf("%d%d",&x,&y);
    srand(time(Null));
    a =( rand() + x)%y ;
    printf("The Randomly Generated Number is %d.\n",a);


}

Assuming you want an exclusive upper bound the right way to do this is: 假设您想要一个排他的上限,正确的方法是:

(rand() % (high - low)) + low

Assuming high=100 and low=90, walking through how this works: 假设高= 100,低= 90,请按照以下步骤操作:

  • you generate a random int 您生成一个随机整数
  • (high - low) gives you the range, 100 - 90 = 10. (高-低)为您提供范围100-90 = 10。
  • When you % by 10, you are going to get a result in the range [0, 10), so it will be one of 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 当%乘以10时,您将得到[0,10)范围内的结果,因此它将是0、1、2、3、4、5、6、7、8或9之一
  • Since what you really want is one of 90, 91, 92, ..., 99 you have to add low back to it 由于您真正想要的是90、91、92,...,99之一,因此您必须向其添加low

The thing to note here is that you will only get [90, 99]. 这里要注意的是,您只会得到[90,99]。 If you want to have an inclusive upperbound so [90, 100] then you want to add 1 to the amount you are modding by 如果您想要一个包含性的上限,所以[90,100]那么您想在您要修改的金额上加1

(rand() % (high + 1 - low)) + low

You have to use this formula for the y exclusive ( a in [x , y[ ): 您必须对y排除使用此公式([x,y [)中的a:

a = (rand() % (y - x)) + x;

and this one for the y inclusive ( a in [x , y] ): 而这个包含y(含[x,y]中的a):

a = (rand() % (y - x + 1)) + x;

Edit: It should be: 编辑:应该是:

if (x == y) a = x
else a = (rand() % (y - x)) + x

assuming you want an actual random distribution... psudeocode 假设您想要一个实际的随机分布... psudeocode

range = high - low
r = rand()
while (r > range)
{
    r=rand();
}
r+=low;

something like that should give you a uniform distribution by rejecting things out of range, because using mod n there isn't a uniform distribution for all values of n over rand's domain ( 0 to RAND_MAX which is 0x7fffffff on my C library). 这样的事情应该通过拒绝超出范围的东西来给您一个统一的分布,因为使用mod n n对于rand的域(从0RAND_MAX ,在我的C库中为0x7fffffff )的n的所有值都没有统一的分布。

lets use the contrived example of 0xf as the domain: 让我们使用人为的0xf示例作为域:

if you do rand() % 10 for every value in the range... then you get: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5. 如果对范围内的每个值都做rand() % 10 ...则得到:0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 ,5。 which has 2* as many 0,1,2,3,4,5 as 6,7,7,8,9... 它的2 *等于6,7,7,8,9的0,1,2,3,4,5 ...

by going to a larger domain you get smoother distribution, but it will always be lumpy if you aren't modding by something that cleanly divides RAND_MAX+1 通过进入更大的域,您可以获得更平滑的分布,但是如果您不擅长采用将RAND_MAX + 1划分为整洁的东西,它将总是块状的

depending on the numbers you are working on and how fast rand is you could do something simplistic like above or you could discard things above the largest number that divides cleanly by your divisor... so discard everything over 0x7FFFFFF8 if you are dividing by 10 to get rid of bias. 根据您正在处理的数字以及兰德有多快,您可以像上面这样简单地做一些事情,也可以丢弃大于除数的最大数的东西...除以0x7FFFFFF8的所有结果,如果除以10摆脱偏见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM