Why always getting same minimum value?

Hi,
I am trying to find minimum value using threads. I am using rand() function but I am always getting the same answer. Following is my code which I retrieved from a book.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include<pthread.h>
#include<limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>

void *find_min(void *list_ptr);
pthread_mutex_t minimum_value_lock;
long minimum_value=9999999999;
int partial_list_size;


void *find_min(void *list_ptr){
   int *partial_list_pointer,my_min, i;
   
   //my_min = INT_MIN;
   partial_list_pointer = (int*) list_ptr;
   if(partial_list_pointer[0] != 0)
      my_min = partial_list_pointer[0];
      printf("my_min=%d ", my_min);
   for(i=0; i<partial_list_size;i++){
      if(partial_list_pointer[i] == 0)
      continue;
      if(partial_list_pointer[i] < my_min){
         my_min = partial_list_pointer[i];
      }
   }
   printf("Inside the thread Method\n");
   pthread_mutex_lock(&minimum_value_lock);
   if(my_min < minimum_value)
         minimum_value = my_min;
   pthread_mutex_unlock(&minimum_value_lock);
   pthread_exit(0);
}


/* ---------------------------------------- MAIN */
int main(int argc, char **argv)
{

        int nt = 10;
	//int ret = 0;
	long i = 0;
	long l = 0;
	long cur = 0;
	int seed = 10;
	long nelems = 100;
	int *list = NULL;
	pthread_t *tids = NULL;
	void *res = NULL;
         /* init the mutex */
	pthread_mutex_init(&minimum_value_lock, NULL);

	/* init lists, list_ptr, partial_list_size */
	list = malloc(sizeof(int) * nelems);
	if (list == NULL) {
		printf("Error : could not init the list\n");
		return -1;
	}
	tids = malloc(sizeof(pthread_t) * nt);
	if (tids == NULL) {
		printf("Error : could not init the tids\n");
		return -1;
	}

        srand(seed);
	for (l = 0; l < nelems; l++) {
		list[l] = (long)(rand());
	}

	if (nt == 1) {
		partial_list_size = nelems;
	} else {
		partial_list_size = (nelems / (long)(nt)) + (nelems % (long)(nt));
	}

        for (i = 0; i < nt; i++) {
		if (pthread_create(&tids[i], NULL, &find_min, &list[cur]) != 0) {
			printf("Error : pthread_create failed on spawning thread %ld\n", i);
			return -1;
		}
		cur += partial_list_size;

		/*
		 * we do this check in order to ensure that our threads
		 * down't go out of bounds of the list
		 * 
		 */
		if ((cur + partial_list_size) > nelems) {
			cur = nelems - partial_list_size;
		}
	}

	/* join threads */
	for (i = 0; i < nt; i++) {
		if (pthread_join(tids[i], &res) != 0) {
			printf("Error : pthread_join failed on joining thread %ld\n", i);
			return -1;
		}
	}
        printf("Minimum value found: %ld\n", minimum_value);
	

	free(list);
	free(tids);
	tids = NULL;
	list = NULL;

	return 0;
}


I am getting the following output:

$ ./a.out
my_min=1424885268 Inside the thread Method
my_min=1215069295 Inside the thread Method
my_min=131589623 Inside the thread Method
my_min=1195282557 Inside the thread Method
my_min=551431658 Inside the thread Method
my_min=2100576643 Inside the thread Method
my_min=1443357700 Inside the thread Method
my_min=210949187 Inside the thread Method
my_min=497686971 Inside the thread Method
my_min=877957347 Inside the thread Method
Minimum value found: 6091026

$ gcc -pthread findMin.c
$ ./a.out
my_min=131589623 Inside the thread Method
my_min=1195282557 Inside the thread Method
my_min=1424885268 my_min=551431658 Inside the thread Method
my_min=1215069295 Inside the thread Method
my_min=1443357700 Inside the thread Method
Inside the thread Method
my_min=2100576643 Inside the thread Method
my_min=210949187 Inside the thread Method
my_min=497686971 Inside the thread Method
my_min=877957347 Inside the thread Method
Minimum value found: 6091026
$

From the above two outputs, it is clear that each time, I am running the program, I am getting the same output.

Some body please guide how to generate a different random series of numbers using rand() function?

Zulfi.
you are using 10 as a seed
the whole point of the seed is that it'll give you the same sequence for the same seed.
Thanks for helping me.

Zulfi.
Topic archived. No new replies allowed.