简体   繁体   English

在 C 中使用 rand() 以在不使用 time() 或 getpid() 的情况下每次运行获得不同的数字?

[英]Use rand() in C to get a different number every run without using time() or getpid()?

I am currently using the time() function inside srand() to generate a completely different and random number everytime the program is run.我目前在 srand() 中使用 time() function 来在每次程序运行时生成一个完全不同的随机数。

srand(time(NULL));// chooses random seed for a different rand() every run 
  int n = 1 + rand() / (RAND_MAX / (100 - 1 + 1) + 1); // only generates numbers between 1 and 100
  printf("n = %d",n);
  int a = rand();
  printf("\na = %d",a);
  int b = rand();
  printf("\nb = %d",b);

Unfortunately, I have learned that I am not allowed to use time() or getpid().不幸的是,我了解到我不允许使用 time() 或 getpid()。 Is there a way, using ONLY <stdio.h>, <stdlib.h> <assert.h> to generate a different random number every time the program is run?有没有办法,每次运行程序时只使用 <stdio.h>, <stdlib.h> <assert.h> 生成不同的随机数?

I think someone has answered your question here: Generating random values without time.h我想有人在这里回答了你的问题: Generating random values without time.h

I think you can use a file with numbers and random your number from there, for example:我认为您可以使用带有数字的文件并从那里随机选择您的数字,例如:

static int randomize_helper(FILE *in)
{
     unsigned int  seed;

    if (!in)
         return -1;

    if (fread(&seed, sizeof seed, 1, in) == 1) {
        fclose(in);
        srand(seed);
    return 0;
    }

    fclose(in);
    return -1;
}

static int randomize(void)
{
    if (!randomize_helper(fopen("/dev/urandom", "r")))
         return 0;
    if (!randomize_helper(fopen("/dev/arandom", "r")))
         return 0;
    if (!randomize_helper(fopen("/dev/random", "r")))
         return 0;

/* Other randomness sources (binary format)? */

/* No randomness sources found. */
    return -1;
}

and this is an example for main:这是主要的一个例子:

 int main(void)
 {
    int I;

    if (randomize())
        fprintf(stderr, "Warning: Could not find any sources for randomness.\n");

    for (i = 0; i < 10; i++)
        printf("%d\n", rand());

    return EXIT_SUCCESS;
}

I'm kind of impressed I haven't seen this method anywhere else, one day I was digging through some github repos, and I found a projet using a method to randomly seed srand() (I forgot which repo).我有点印象深刻,我在其他任何地方都没有见过这种方法,有一天我正在挖掘一些 github 存储库,我发现了一个使用随机播种 srand() 的方法的项目(我忘记了哪个存储库)。

I find it very elegant, I don't know too much about how the computer decides where each function should be, but this seems to work very well, if anyone wants to correct/clarify, it would be great.我觉得它非常优雅,我不太了解计算机如何决定每个 function 应该在哪里,但这似乎工作得很好,如果有人想纠正/澄清,那就太好了。

void seed(void)
{
    return;
}

int main(void)
{
    srand((unsigned long)&seed);
    return (0);
}

This was how I found it, but I've tried passing it the address of main instead of the address of seed and it seems to work too.这就是我找到它的方式,但我尝试将 main 的地址而不是种子的地址传递给它,它似乎也可以工作。

You don't really need a specific seed function, any function will do.您真的不需要特定的种子 function,任何 function 都可以。

Now I'm pretty sure this is not a reliable method for pseudo-randomness but for small projects that needs just the simples implementation of something "random", without using time(), getpid() or a block of code, this will more than do.现在我很确定这不是一种可靠的伪随机方法,但对于只需要简单实现“随机”的东西,而不使用 time()、getpid() 或代码块的小型项目,这将更多比做。

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

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