Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c程序设计中算术级数的随机数生成_C - Fatal编程技术网

c程序设计中算术级数的随机数生成

c程序设计中算术级数的随机数生成,c,C,这是我现在的代码: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { float r; srand((unsigned)time(NULL)); r = (float)rand()/RAND_MAX*20; printf("%.1f\n",r); return(0); } #包括 #包括 #包括 int main() {

这是我现在的代码:

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

int main()
{ 
    float r;

    srand((unsigned)time(NULL));

    r = (float)rand()/RAND_MAX*20;
    printf("%.1f\n",r);


   return(0);
}
#包括
#包括
#包括
int main()
{ 
浮子r;
srand((无符号)时间(NULL));
r=(浮点)rand()/rand_MAX*20;
printf(“%.1f\n”,r);
返回(0);
}
这段代码可以生成一个小数点在0-20之间的随机数,但是我想生成一个像0.5,1.0,1.5,2.0,2.5…19.5,20.0这样的数字


我应该在我的程序中添加什么?

要在
a
b
之间生成一个随机数,并且
n
具有不同的、均匀间隔的可能值:

 value = a + ((b - a) / (n - 1)) * (rand() % n);
因此,对于0.5,1.0…,20.0,使用
a=0.5,b=20.0,n=40


在使用这种方法时,通常要注意模偏差;但是,
rand()
并不是一个足够好的生成器,它无论如何都不足以产生这个问题……

a
b
之间生成一个随机数,其中
n
是不同的、均匀分布的、可能的值:

 value = a + ((b - a) / (n - 1)) * (rand() % n);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{ 
    float r;

    srand((unsigned)time(NULL));

    r = (float)rand()/RAND_MAX*20;
    d = (float)rand()/RAND_MAX*1;
    if (d == 1) {
        r = r + 0.5;
    }
    printf("%.1f\n",r);


   return(0);
}
因此,对于0.5,1.0…,20.0,使用
a=0.5,b=20.0,n=40

在使用这种方法时,通常要注意模偏差;但是,
rand()
并不是一个足够好的生成器,它无论如何都不会影响到这一点……

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

int main()
{ 
    float r;

    srand((unsigned)time(NULL));

    r = (float)rand()/RAND_MAX*20;
    d = (float)rand()/RAND_MAX*1;
    if (d == 1) {
        r = r + 0.5;
    }
    printf("%.1f\n",r);


   return(0);
}
#包括 #包括 int main() { 浮子r; srand((无符号)时间(NULL)); r=(浮点)rand()/rand_MAX*20; d=(浮点)rand()/rand_MAX*1; 如果(d==1){ r=r+0.5; } printf(“%.1f\n”,r); 返回(0); }
或者如果您想要2.3或5.4这样的数字:

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

int main()
{ 
    float r;

    srand((unsigned)time(NULL));

    r = (float)rand()/RAND_MAX*200;
    r /= 10;
    printf("%.1f\n",r);


   return(0);
}
#包括
#包括
#包括
int main()
{ 
浮子r;
srand((无符号)时间(NULL));
r=(浮点)rand()/rand_MAX*200;
r/=10;
printf(“%.1f\n”,r);
返回(0);
}
#包括
#包括
#包括
int main()
{ 
浮子r;
srand((无符号)时间(NULL));
r=(浮点)rand()/rand_MAX*20;
d=(浮点)rand()/rand_MAX*1;
如果(d==1){
r=r+0.5;
}
printf(“%.1f\n”,r);
返回(0);
}
或者如果您想要2.3或5.4这样的数字:

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

int main()
{ 
    float r;

    srand((unsigned)time(NULL));

    r = (float)rand()/RAND_MAX*200;
    r /= 10;
    printf("%.1f\n",r);


   return(0);
}
#包括
#包括
#包括
int main()
{ 
浮子r;
srand((无符号)时间(NULL));
r=(浮点)rand()/rand_MAX*200;
r/=10;
printf(“%.1f\n”,r);
返回(0);
}

我会将随机数保持为
int
,并在1到40之间生成它。然后除以2.0得到你想要的数字

比如:

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

int main()
{ 
    float r;
    int n;

    srand((unsigned)time(NULL));

    for (int i=0; i<10; ++i)
    {
        n = rand() % 40 + 1;  // Gives random values 1, 2, …, 39, 40
        r = n/2.0;            // Divide by floating point 2 to get 0.5, 1.0, .. 20.0
        printf("%.1f\n",r);
    }

   return(0);
}

我将随机数保持为
int
并生成它在1到40之间。然后除以2.0得到你想要的数字

比如:

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

int main()
{ 
    float r;
    int n;

    srand((unsigned)time(NULL));

    for (int i=0; i<10; ++i)
    {
        n = rand() % 40 + 1;  // Gives random values 1, 2, …, 39, 40
        r = n/2.0;            // Divide by floating point 2 to get 0.5, 1.0, .. 20.0
        printf("%.1f\n",r);
    }

   return(0);
}

这不是非常随机这不是非常随机一般来说,
rand()%40+1
不提供均匀分布。如果
RAND_MAX
设置为32767,则[1,7]中的数字以820/32767的概率返回,而[8,40]中的数字以819/32767的概率返回。@Giovanni基本形式的
RAND
确实没有提供统一的分布,但。。。你真的认为这就是OP所关心的吗?一般来说,
rand()%40+1
并不能提供统一的分布。如果
RAND_MAX
设置为32767,则[1,7]中的数字以820/32767的概率返回,而[8,40]中的数字以819/32767的概率返回。@Giovanni基本形式的
RAND
确实没有提供统一的分布,但。。。你真的认为这就是OP所关心的吗?第一个代码不编译,用
float d修改,它生成13.1、9.7等值(不是OP目标的一部分),并且明显没有统一创建20.0。第一个代码不编译,用
float d修改,它生成13.1、9.7等值(不是OP目标的一部分),并且明显没有统一创建20.0。