Why does srand create same numbers?

回眸只為那壹抹淺笑 提交于 2020-01-24 21:14:41

问题


I wrote a program in c that creates randomly matrix. It creates a string like this (3,-6,2;5,2,-9;-8,20,7). ";" cuts every row and a "," every column. Now i wrote a rust program that makes matrixaddition or mult. I call it with this:
./matrix ./test 3 3 "*" ./test 3 3

./matrix calls my rust program and i give it 3 arguments. (Matrix 1, Operator, Matrix2) It works and the calculation is fine but Matrix 1 and 2 are always equal. I think it is because I use srand depending on time and because i call it at the same time it creates two times the same. I also tested the Matrixrandomizer without including it in my rust call and it always creates different matrix.

Here you can see my c Code.

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

int main (int argc, char* argv[]) {
    // Zufallszahlengenerator initialisieren
    srand(time(NULL));

    if(argc < 3) {
        printf("Es fehlen Argumente");
    }   
    char matrix[100] = "";

    int r, c;
    r = atoi(argv[1]);
    c = atoi(argv[2]);

    if(r > 0 && c > 0) {
        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++){
                if(j == c - 1) {
                    int test = (1+rand()%9);
                    char buffer[50];
                    sprintf(buffer, "%d", test);
                    strcat(matrix, buffer);
                    }
                if(j < c - 1){  
                    int test = (1+rand()%9);
                    char buffer[50];
                    sprintf(buffer, "%d", test);
                    strcat(matrix, buffer);
                    strcat(matrix, ",");    
                }

            }
            if(i != r - 1) {
                strcat(matrix, ";");
            }   
        }       
    }
    printf("%s", matrix);
}


回答1:


srand wants a seed as argument. For a given seed, the random sequence will be equal. So if you call srand twice in a very short time with time(NULL) as argument, chances are high that you will give the same seed.

Using time(NULL) as seed is a good way to ensure different output each time you run the program during normal circumstances. But in this case you need to add something more. One way of doing it is to add an extra argument to work as salt (terminology borrowed from hashing) like this:

int salt = atoi(argv[3]);
srand(time(NULL) + salt);

And then make sure to call the program with different arguments, like this:

./test 3 3 546

Here 546 is a number you choose, and it should be different for the two calls. And I would recommend making them very different. If they differ by just one, you may get the same problem if the clock changes second between the two calls.

Another way of doing it is to use getpid() as salt. This may be preferable if you are not willing to alter the number of arguments you are supposed to send to the program.




回答2:


The srand function takes a seed as argument. For the same seed, the pseudo-random number generator will always provide the same output sequence. There is a minimum time step that can be recognized in any discrete system such as computers. Hence, calling /matrix ./test 3 3 "*" ./test 3 3 could happen at a smaller time step, which means they're happening at the same time and time(NULL) returns the same time, the same seed, therefore the same random sequence and hence, the same matrices. A quick solution is to try srand(time(NULL) + getpid()) and #include <unistd.h> in a POSIX system, or srand(time(NULL) + GetCurrentProcessId()) for Windows systems. A more elaborate solution is to provide with salt your initial seed, a salt is basically a value that gets added to the seed. However, choosing the salt value is a different topic altogether. You can find pointers for it here.



来源:https://stackoverflow.com/questions/58580795/why-does-srand-create-same-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!