-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathser_rand.c
More file actions
87 lines (72 loc) · 2.16 KB
/
ser_rand.c
File metadata and controls
87 lines (72 loc) · 2.16 KB
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
/* File:
* ser_rand.c
*
* Purpose:
* Generate random numbers with function that isn't threadsafe.
*
* Compile:
* gcc -g -Wall -o ser_rand ser_rand.c -lpthread
* Usage:
* ser_rand <thread_count> <number of random numbers per thread>
*
* Input:
* None
* Output:
* Random numbers from each thread
*
* Warning:
* The My_random function is *not* threadsafe.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define MR_MULTIPLIER 279470273
#define MR_MODULUS 4294967291U
void Usage(char* prog_name);
unsigned My_random(unsigned seed);
/*--------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
long thread;
int thread_count;
int n, i;
if (argc != 3) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
n = strtol(argv[2], NULL, 10);
for (thread = 0; thread < thread_count; thread++) {
My_random(thread + 1); /* "Seed" Random number generator */
for (i = 0; i < n; i++)
printf("Th %ld > %u\n", thread, My_random(0));
}
return 0;
} /* main */
/*--------------------------------------------------------------------
* Function: Usage
* Purpose: Print command line for function and terminate
* In arg: prog_name
*/
void Usage(char* prog_name) {
fprintf(stderr, "usage: %s <thread count> <number of random vals per thread>\n",
prog_name);
exit(0);
} /* Usage */
/*-------------------------------------------------------------------
* Function: My_random
* Purpose: Generate random numbers
* In arg: seed (ignored if = 0)
* Ret val: A "random" unsigned int
*
* Note: The first time the function is called, the random
* number should be "seeded" with a nonzero argument,
* and the return value ignored. Subsequent calls
* should have a zero argument and the return values
* will form a "pseudo-random" sequence.
*/
unsigned My_random(unsigned seed) {
static unsigned z;
unsigned long tmp;
if (seed != 0) z = seed;
tmp = z*MR_MULTIPLIER;
z = tmp % MR_MODULUS;
return z;
} /* My_random */