D.Knuth 伪随机发生器的使用

发布于 2024-09-28 01:04:19 字数 712 浏览 4 评论 0原文

我需要编写一个物理模拟软件,并且需要使用 由 D.Knuth 编写的伪随机生成器,因为它最适合我的需求。

虽然我知道如何在硬代码块中使用它(仅仅是#include“rng-double.c”),但我不太明白如何将它包含在主程序和主程序中都需要的模块化程序中。在一些库中,然后我通过头文件链接回主库。

我需要的唯一函数是 ranf_start(seed) 来为生成器提供种子,并 ranf_arr_next() 来生成下一个数字。

我都得到了一系列的信息

multiple definitions of 'foo'

对于随机数生成器代码中定义的每个函数,

。我不是专家,所以我发现自己几乎陷入了死胡同。有出路吗?我目前正在尝试为 rng 编写一个头文件,尽管尚未成功。

非常感谢大家。 〜J

编辑: 根据第一个答案所说,我需要编写头文件。我对生成器中的大多数函数都这样做了,但我无法为实际生成数字的函数正确编写它。我应该如何编写这样定义的函数的标头?

#define ranf_arr_next() (*ranf_arr_ptr>=0? *ranf_arr_ptr++: ranf_arr_cycle())

I need to write a physical simulation software, and I need to use the pseudo-random generator written by D.Knuth, cause it best fits my needs.

Though I know how to use it within a hard block of code (a mere #include "rng-double.c"), I can't quite understand how to include it in a modular program where it is needed both within the main and inside some libraries that I then link back into the main through header files.

The only functions that I need are ranf_start(seed) to seed the generator and ranf_arr_next() to generate the next number.

I'm getting a long series of

multiple definitions of 'foo'

pretty much for every function that is defined in the random number generator code.

I'm not an expert, so I find myself pretty much at a dead end. Is there a way out? I am currently trying to write an header file for the rng, though I haven't been successful yet.

Thanks a lot, everybody.
~J

EDIT:
From what the first answers say, I need to wirte the header file. I did it for most functions in the generator, but I cant manage to write it right for the function that actually generates the number. How should I write the header for a function defined like this?

#define ranf_arr_next() (*ranf_arr_ptr>=0? *ranf_arr_ptr++: ranf_arr_cycle())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-10-05 01:04:19

您不包含 .c 文件,而是包含相应的头文件,其中包含函数的声明。 .c 文件将与其余文件一起编译。

You don't include the .c file, but the appropriate header file, which contains the declarations of your functions. The .c file will just be compiled along with the rest.

微凉徒眸意 2024-10-05 01:04:19

如果您发现自己 #include 一个 .c 文件,那么很可能您做错了什么。你真的应该只#include .h 文件。

此维基百科链接准确解释了您遇到的问题以及如何正确编写头文件。

我怀疑你的问题是你没有正确使用标题“guard”。

If you ever find yourself #including a .c file, then chances are that you are doing something wrong. You really should be #including .h files only.

This wikipedia link explains exactly the problem you are having and how to write the header file properly.

I suspect your problem is that you are not using the header 'guard' properly.

还如梦归 2024-10-05 01:04:19

rng-double.h

#ifndef RNG_DOUBLE_H_INCLUDED
#define RNG_DOUBLE_H_INCLUDED

extern void ranf_start(long seed);
extern double ranf_next(void);

#ifdef USE_MACRO_RANF_NEXT
extern double *ranf_arr_ptr;
extern double  ranf_arr_cycle(void);
#define ranf_arr_next() (*ranf_arr_ptr >= 0 ? *ranf_ptr++ : ranf_arr_cycle())
#endif /* USE_MACRO_RANF_NEXT */

#endif /* RNG_DOUBLE_H_INCLUDED */

main.c

#include "rng-double.h"
...other stuff...

rng-double.c

#define USE_MACRO_RANF_NEXT
#include "rng-double.h"

double *ranf_arr_ptr;

void ranf_start(long seed)
{
    ...implementation...
}

double (ranf_next)(void)  // Function
{
    ranf_next();          // Macro
}

double ranf_arr_cycle(void)
{
    ...implementation...
}

rng-double.h

#ifndef RNG_DOUBLE_H_INCLUDED
#define RNG_DOUBLE_H_INCLUDED

extern void ranf_start(long seed);
extern double ranf_next(void);

#ifdef USE_MACRO_RANF_NEXT
extern double *ranf_arr_ptr;
extern double  ranf_arr_cycle(void);
#define ranf_arr_next() (*ranf_arr_ptr >= 0 ? *ranf_ptr++ : ranf_arr_cycle())
#endif /* USE_MACRO_RANF_NEXT */

#endif /* RNG_DOUBLE_H_INCLUDED */

main.c

#include "rng-double.h"
...other stuff...

rng-double.c

#define USE_MACRO_RANF_NEXT
#include "rng-double.h"

double *ranf_arr_ptr;

void ranf_start(long seed)
{
    ...implementation...
}

double (ranf_next)(void)  // Function
{
    ranf_next();          // Macro
}

double ranf_arr_cycle(void)
{
    ...implementation...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文