D.Knuth 伪随机发生器的使用
我需要编写一个物理模拟软件,并且需要使用 由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不包含 .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.
如果您发现自己 #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.
rng-double.h
main.c
rng-double.c
rng-double.h
main.c
rng-double.c