C编程,GSL vector 库,错误: incompatible type for argument 1 of 'gsl_rng_uniform'

标签 c gsl

我正在 Windows 操作系统上的代码块中进行编程。我必须在下面的代码中使用一些 GSL 库,但在第 13 行出现错误,'gsl_rng_uniform 的参数 1 的类型不兼容

double randomBR = gsl_rng_uniform(r)*360+6;

我不知道如何解决它。在错误消息下,gsl_rng.h 文件中的第 150 行注释显示:“expected 'const struct gsl_rng *' but argument is of type 'double'”。

#include <stdio.h>
#include <gsl_vector.h>
#include <gsl_rng.h>

int main()
{
gsl_vector*vec=gsl_vector_alloc(100);

double z, max, min, r;
int k=100, i, pos;

for (i=0; i<k; i++){
    double randomBR = gsl_rng_uniform(r)*360+6;
    z=((int)(gsl_rng_uniform)(r)*360+6);
    gsl_vector_set(vec, i, z);
}

max=gsl_vector_max(vec);
pos_vector_max_index(vec);
min=gsl_vector_min)(vec);
pos_vector_min_index(vec);

if (max == 360)
{
    printf("Max number generated in box %d", pos);
}

if (max < 366)
{
    printf("Max number is %f, position %d", max, pos);
}

return 0;
}

最佳答案

函数原型(prototype)为

double gsl_rng_uniform (const gsl_rng * r)

显然它需要一个 const gsl_rng*,而不是一个 double。 阅读有关 GSL 文档的更多信息,您必须首先使用 gsl_rng_alloc 初始化它来获取 rng:

gsl_rng * r = gsl_rng_alloc (gsl_rng_taus);

然后将 gsl_rng r 传递给统一函数以获得所需的随机数。

尝试阅读此处的文档:

http://www.gnu.org/software/gsl/manual/html_node/Random-number-generator-initialization.html#Random-number-generator-initialization

http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html

关于C编程,GSL vector 库,错误: incompatible type for argument 1 of 'gsl_rng_uniform' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611927/

相关文章:

c - 不小心创建了巨大的文件

c - C中带指针的嵌套结构

C泛型编程

c++ - 需要使用 gsl LU 分解获取方阵逆矩阵的示例代码

c - 使用 WTO 在 Metal C 中打印

c - GTK+ 2.24。如何在C中使用unicode符号

c - 基本线性代数算法(MATLAB 和 C)

c - 警告 : assignment from incompatible pointer type [-Wincompatible-pointer-types] qt1. 函数 = &H1

c - GSL 特征值顺序

c - 在 C 中调用常数复数的最快方法