C - HOW TO ENSURE DEVELOPMENT of random number generation in C when the program is executed within one second?

For the assignment, I have to make sure that even when I execute the program for one second, I have to return different numbers. However, I read other posts and cannot figure out how to do this in one second. If I run the code:

int main()
{    
    srand(time(NULL));

    for(count = 0; count < 10; count++)
    {
        printf("%d\n", rand()%20 + 1);
    }

    return 0;
}

As a result, I get the same result when executed for one second. Does anyone know how to mix results in one second? I work in a Unix environment if that matters. Thanks.

+4
source share
5 answers

2 rand(), , , srand().

2-

1) - .
2) , .

1A # 1 time(), , .

1B , , - " - 12345, , , ".

1C , (@Will). , .

2A , , rand(), srand(). .
[Edit] Unix .
/dev/random /dev/urandom (@Dietrich Epp) .

2B ,

printf("Press enter\n");
unsigned u = 0;
while (!keyboard_hit()) u++;
srand(u);

. ( ^) , pid(), time(), react_time() .. (. @nodakai)


2 - : 1 20 ^ 10 (10 240 000 000 000) OP. , , .

, 2 - , . , - , , , , , , .

// pseudo code
n = 1;
repeat {
  srand(time()^n^pid());
  n++;
  generate_random_number_sequence();
  attempt exclusive r/w access to shared file.
  if (file opened) {
    read file;
    if (different sequence) {
      write new sequence and fclose()
      if (no I/O errors) {
        we are done - exit
      }
    }
    fclose()
  }
  maybe sleep for a fraction of a second
  maybe quit if repeated too often
}
+4

/dev/random /dev/urandom, . , , PRNG.

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
// Seed the random number generator.
void seed_rng(void)
{
    int fp = open("/dev/random", O_RDONLY);
    if (fp == -1) abort();
    unsigned seed;
    unsigned pos = 0;
    while (pos < sizeof(seed)) {
        int amt = read(fp, (char *) &seed + pos, sizeof(seed) - pos);
        if (amt <= 0) abort();
        pos += amt;
    }
    srand(seed);
    close(fp);
}

, /dev/random . .

, , rand() , . RNG , , - , , , rand().

+2

, chux.

? . , , PID, , , , .

"" , (srand() uint, 32 , pid : , 32k); , pid - ,

srand(getpid() + (time(0) << (CHAR_BIT * sizeof(pid_t))))
+2
  • PID ( ) , () . Linux, cat /proc/sys/kernel/pid_max .

    #include <sys/types.h>
    #include <unistd.h>
    
    unsigned seed = ((unsigned)getpid() << 16) | (0xFFFFu & (unsigned)time(NULL));
    

    , getpid() time(), getpid() , .

  • ( . .). rand, random/srandom BSD.

  • ( OP. .) , , 10 000 , PRNG.

    srand(time(NULL)); /* or whatever initialization */
    for(count = 0; count < 10000; count++) rand();
    
    for(count = 0; count < 10; count++)
    {
        printf("%d\n", rand()%20 + 1); /* or whatever random function */
    }
    

(, ) , , , -.

#include <stdio.h>
#include <stdlib.h>

/* http://en.wikipedia.org/wiki/Linear_congruential_generator */
int doTest(int N) {
    int i;
    unsigned init, r;

    for (init = 1; init <= 10; ++init) {
        r = init;
        for (i = 0; i < N; ++i)
            r = 22695477 * r + 1;
        printf("%2d -> ...%3d times... -> %6u\n", init, N, (r>>16) & ((1<<15)-1));
    }
    printf("\n");
}

int main(void) {
    doTest(1);
    doTest(2);
    doTest(3);
    doTest(10000);
    return 0;
}

r = 22695477 * r + 1 (r>>16) & ((1<<15)-1) - , , Borland C/++ rand (. wikipedia.)

PRN - .

 1 -> ...  1 times... ->    346
 2 -> ...  1 times... ->    692
 3 -> ...  1 times... ->   1038
 4 -> ...  1 times... ->   1385
 5 -> ...  1 times... ->   1731
 6 -> ...  1 times... ->   2077
 7 -> ...  1 times... ->   2424
 8 -> ...  1 times... ->   2770
 9 -> ...  1 times... ->   3116
10 -> ...  1 times... ->   3463

 1 -> ...  2 times... ->    130
 2 -> ...  2 times... ->  32682
 3 -> ...  2 times... ->  32467
 4 -> ...  2 times... ->  32251
 5 -> ...  2 times... ->  32036
 6 -> ...  2 times... ->  31820
 7 -> ...  2 times... ->  31604
 8 -> ...  2 times... ->  31389
 9 -> ...  2 times... ->  31173
10 -> ...  2 times... ->  30957

 1 -> ...  3 times... ->  10982
 2 -> ...  3 times... ->  21834
 3 -> ...  3 times... ->  32686
 4 -> ...  3 times... ->  10770
 5 -> ...  3 times... ->  21622
 6 -> ...  3 times... ->  32473
 7 -> ...  3 times... ->  10557
 8 -> ...  3 times... ->  21409
 9 -> ...  3 times... ->  32261
10 -> ...  3 times... ->  10345

 1 -> ...10000 times... ->  13125
 2 -> ...10000 times... ->   5687
 3 -> ...10000 times... ->  31016
 4 -> ...10000 times... ->  23578
 5 -> ...10000 times... ->  16140
 6 -> ...10000 times... ->   8702
 7 -> ...10000 times... ->   1264
 8 -> ...10000 times... ->  26594
 9 -> ...10000 times... ->  19156
10 -> ...10000 times... ->  11718
+1
source

You can use the clock () function, which (according to http://www.cplusplus.com/reference/ctime/clock/ ) returns the number of measures since the process started. Usually this depends on calls to the same function, so even if the program is called in seconds, there will be a slight time difference that will cause your RNG to return different numbers.

srand( time(NULL) * clock() );
0
source

Source: https://habr.com/ru/post/1525544/


All Articles