使用D.Knuth伪随机发生器的 [英] use of D.Knuth pseudo-random generator
问题描述
我需要写一个物理仿真软件,我需要使用的写有nofollow的>伪随机数发生器,因为它最适合我的需要。
虽然我知道如何code的硬块中使用它(仅仅的#includeRNG-double.c),我不是很了解如何将其纳入一个模块化程序它在哪里在双方的主要和一些图书馆里面需要我再通过头文件链接到主。
这是我唯一需要的功能是ranf_start(种子)种子的发电机和ranf_arr_next()生成一个号码。
我收到了一长串的
'富'的多个定义
pretty多的是在随机数生成器code定义的每个功能。
我不是专家,所以我发现自己pretty多在一个死胡同。有什么办法呢?我目前正在写的一个RNG的头文件,虽然我还没有取得成功呢。
非常感谢大家。
〜Ĵ
编辑:
从什么第一的答案说,我需要WIRTE头文件。我这样做是在发电机的大部分功能,但我不能管理它写正确的,实际上产生号码的功能。我应该如何写这样定义的函数的头?
的#define ranf_arr_next()(* ranf_arr_ptr> = 0 * ranf_arr_ptr ++:ranf_arr_cycle())
RNG-double.h
的#ifndef RNG_DOUBLE_H_INCLUDED
#定义RNG_DOUBLE_H_INCLUDEDEXTERN无效ranf_start(long种子);
EXTERN双ranf_next(无效);#IFDEF USE_MACRO_RANF_NEXT
双的extern * ranf_arr_ptr;
EXTERN双ranf_arr_cycle(无效);
#定义ranf_arr_next()(* ranf_arr_ptr> = 0 * ranf_ptr ++:ranf_arr_cycle())
#ENDIF / * * USE_MACRO_RANF_NEXT /#ENDIF / * * RNG_DOUBLE_H_INCLUDED /
的main.c
的#includeRNG-double.h
...其他的东西...
RNG-double.c
的#define USE_MACRO_RANF_NEXT
#包括RNG-double.h双* ranf_arr_ptr;无效ranf_start(long种子)
{
... ...实施
}双(ranf_next)(无效)//函数
{
ranf_next(); //宏
}双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())
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...
}
这篇关于使用D.Knuth伪随机发生器的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!