1

我正在编写一个使用 GSL 的随机数生成器的程序,当我尝试将随机数生成器的实例传递给函数时出现分段错误。这是我的源代码:

int main(void)
{
    gsl_rng *r;
    int deck[52];
    int count = 0;

    r = gsl_rng_alloc(gsl_rng_mt19937);
    gsl_rng_set(r, time(NULL));

    // Initialize a custom deck 
    // code omitted...    
    // Perform trials
    for (int j = 0; j < NUMTRIALS; j++) {
        shuffle_two(r, deck);
        if (deck[NUMCARDS-1] + deck[NUMCARDS-2] == 11)
            count++;
    }

    // Report result
    cout << fixed << setprecision(6) << count/static_cast<double>(NUMTRIALS);
    cout << endl;

    gsl_rng_free(r);
}

void shuffle_two(gsl_rng* r, int deck[])
{
    double u;
    int bottom, random;
    int temp_card;

    for (int i = 0; i < 2; i++) {
        u = gsl_rng_uniform(r);
        //code for shuffling goes here
    }
}

显然 r 的值在算法运行时会发生变化。当我进行回溯时,我得到的 r 有时为空,有时为 0xa。我不确定为什么。我认为这可能与gsl_rng_uniform 函数的 const 指针参数有关,如此处所述。

这是调试器的输出:

程序收到信号 SIGSEGV,分段错误。
gsl_rng_uniform (r=0x0) at ../gsl/gsl_rng.h:167
167 ../gsl/gsl_rng.h:没有这样的文件或目录。
    在 ../gsl/gsl_rng.h
(gdb) 回溯
#0 gsl_rng_uniform (r=0x0) at ../gsl/gsl_rng.h:167
#1 0x0000000000400d97 in shuffle_two (r=0x0, deck=0x7fffffffdfd0)
    在 blackjack.cpp:55
#2 0x0000000000400cad in main () at blackjack.cpp:33
(gdb)
4

0 回答 0