【发布时间】:2014-11-02 10:10:11
【问题描述】:
我正在构建一个包含 C++ 代码的 R 包。其中,我使用 rand() 函数。该软件包在我的 Linux 机器上检查和构建时没有任何异常。但是,当我尝试使用 Windows builder 检查 Windows 构建时,我收到以下警告:
* checking compiled code ... NOTE
File 'tagcloud/libs/i386/tagcloud.dll':
Found 'rand', possibly from 'rand' (C)
Object: 'overlap.o'
Found 'srand', possibly from 'srand' (C)
Object: 'overlap.o'
File 'tagcloud/libs/x64/tagcloud.dll':
Found 'rand', possibly from 'rand' (C)
Object: 'overlap.o'
Found 'srand', possibly from 'srand' (C)
Object: 'overlap.o'
Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console, nor the C RNG.
下面是一些示例代码:
#include "Rcpp.h"
#include <cstdlib>
#include <time.h>
using namespace Rcpp;
RcppExport SEXP test( ) {
double x ;
srand( (unsigned) time(NULL) );
x = rand();
return( 1 );
}
我不明白有关 rand() 的抱怨。我也没有看到我在代码中的哪个位置“调用可能终止 R 的入口点”或“写入 stdout/stderr”,但是,如果我删除对 rand/srand 的调用,这条消息就会消失。
Google 在其检查日志中显示了许多似乎具有相同注释的软件包。你看到了吗?有没有办法摆脱它?
【问题讨论】:
-
我认为这是你应该担心的最后一个——编译后的代码不应该调用 C RNG。
-
我很困惑。为什么我不应该使用 C 随机数生成器?我应该改用什么?
-
我不知道你第一个问题的答案,但可能与种子管理有关,但Rcpp has full access to R style p/d/r/q distributional functions via syntactic sugar。
-
很好,但我不需要那个;我没有做任何统计计算,我只需要一点随机性来生成视觉标签云。
-
好吧,你没有有使用以这种方式生成的随机数进行统计计算。