Skip to content

Commit

Permalink
Fix multi-thread rand()
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAB committed Mar 7, 2017
1 parent b3a3e92 commit 4422399
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,23 @@ char **get_random_paths_indexes(char **paths, int n, int m, int *indexes)
}
*/

int mt_seed = 0;

char **get_random_paths(char **paths, int n, int m)
{
char **random_paths = calloc(n, sizeof(char*));
int i;
pthread_mutex_lock(&mutex);
if (mt_seed == 0) mt_seed = time(0);
srand(mt_seed);
//printf("n = %d \n", n);
for(i = 0; i < n; ++i){
int index = (rand()*rand())%m;
for(i = 0; i < n; ++i){
int index = rand()%m;
random_paths[i] = paths[index];
//if(i == 0) printf("%s\n", paths[index]);
//printf("%s\n", paths[index]);
//printf("grp: %s\n", paths[index]);
}
mt_seed = rand();
pthread_mutex_unlock(&mutex);
return random_paths;
}
Expand Down

0 comments on commit 4422399

Please sign in to comment.