简体   繁体   English

为随机全局变量播种rand()

[英]Seeding rand() for random global variables

I'm trying to use C's rand() to initialize random inputs for a pseudo-random number generator. 我正在尝试使用C的rand()初始化伪随机数生成器的随机输入。 Due to constraints of a PRNG testing library I'm using, my generator function cannot accept any parameters, so it seems like I'll need to store the initial values as global variables. 由于我使用的PRNG测试库的限制,我的生成器函数不能接受任何参数,因此似乎需要将初始值存储为全局变量。

Ideally, I would seed the generator with srand(time(NULL)) , but this throws the "initializer element is not a compile-time constant" error when I try to do it globally. 理想情况下,我将使用srand(time(NULL))作为生成器的种子,但是当我尝试在全局范围内执行此操作时,将引发“ initializer元素不是编译时常量”错误。

What's the most straightforward way to do this? 最简单的方法是什么? So far I've come up with passing the global variables into a function and doing the work there, like this: 到目前为止,我已经提出将全局变量传递到函数中并在其中进行工作,如下所示:

unsigned int* w;
unsigned int* x;
unsigned int* y;
unsigned int* z;

void seed (unsigned int* first, unsigned int* second, unsigned int* third, unsigned int* fourth)
{
    srand((unsigned int) time(NULL));
    unsigned int a = rand();
    unsigned int b = rand();
    unsigned int c = rand();
    unsigned int d = rand();

    first =  &a;
    second = &b;
    third = &c;
    fourth = &d;
}

However, when I try to access my values in main, I get a EXC_BAD_ACCESS error in Xcode: 但是,当我尝试访问main中的值时,我在Xcode中收到一个EXC_BAD_ACCESS错误:

int main (void)
{
    seed(w, x, y, z);
    printf("%i", *w);     // throws error
...
}

... which I'm guessing has something to do with scope and the memory being freed before I want it to be. ...我猜这与范围和在我希望它释放之前释放的内存有关。 Don't have a ton of experience with C, but is this the right approach? 没有大量的C经验,但这是正确的方法吗? If so how can I fix this error? 如果是这样,我该如何解决该错误?

Thanks! 谢谢!

You're assigning pointers to values that exist only on the stack, not pushing things back like you think you are. 您正在将指针分配给仅存在于堆栈中的值,而不是像您认为的那样将其推回去。 Once that stack goes out of scope you're into dangerous territory. 一旦堆栈超出范围,您将陷入危险境地。

Here's how it should be written: 这是应该如何写的:

void seed (unsigned int* a, unsigned int* b, unsigned int* c, unsigned int* d)
{
    srand((unsigned int) time(NULL));
    *a = rand();
    *b = rand();
    *c = rand();
    *d = rand();
}

int main() {
   // Note: These can be global, they're just put here for convenience
   // Remember, global variables are bad and you want to avoid them.
   unsigned int a, b, c, d;
   seed(&a, &b, &c, &d);

   // ...
}

The scope of variable a, b, c, d are within the seed function, so accessing those references outside the function will result unexpected results. 变量a, b, c, d的范围在种子函数内,因此访问函数外部的那些引用将导致意外的结果。
Either you need to pass address of variable to fill or allocate memory for each number in seed function. 您需要传递变量的地址以填充或为seed函数中的每个数字分配内存。

Try below snippet 尝试以下代码段

unsigned int w;
unsigned int x;
unsigned int y;
unsigned int z;

void seed (unsigned int* first, unsigned int* second, unsigned int* third, unsigned int* fourth)
{
    srand((unsigned int) time(NULL));

    *first =  rand();
    *second = rand();
    *third = rand();
    *fourth = rand();
}


int main (void)
{
    seed(&w, &x, &y, &z);
    printf("%i", w);
}

EXC_BAD_ACCESS is because you are assigning address of local variables from seed() to your pointers EXC_BAD_ACCESS是因为您正在将局部变量的地址从seed()分配给指针

These variable and their addresses will be invalid when seed() returns seed()返回时,这些变量及其地址将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM