forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrnd.c
More file actions
99 lines (86 loc) · 1.66 KB
/
Copy pathrnd.c
File metadata and controls
99 lines (86 loc) · 1.66 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
88
89
90
91
92
93
94
95
96
97
98
99
/*
rnd.c
Generate random numbers
*/
#define _POSIX_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *progname;
static void fatal(char *fmt, ...);
static void usage(void);
int main(int argc, char *argv[])
{
int c, i, count;
unsigned long n, v, high, modulus;
unsigned seed;
char *check;
char *c_arg, *m_arg, *s_arg;
(progname=strrchr(argv[0],'/')) ? progname++ : (progname=argv[0]);
c_arg= m_arg= s_arg= NULL;
while (c= getopt(argc, argv, "?c:m:s:"), c != -1)
{
switch(c)
{
case 'c': c_arg= optarg; break;
case 'm': m_arg= optarg; break;
case 's': s_arg= optarg; break;
default:
fatal("getopt failed: '%c'", c);
}
}
if (optind != argc)
usage();
if (c_arg)
{
count= strtol(c_arg, &check, 0);
if (check[0] != '\0')
fatal("bad count '%s'", c_arg);
}
else
count= 1;
if (m_arg)
{
modulus= strtoul(m_arg, &check, 0);
if (check[0] != '\0' || modulus == 0)
fatal("bad modulus '%s'", m_arg);
n= 0x80000000UL / modulus;
if (n == 0)
fatal("bad modulus %lu (too big)", modulus);
high= n * modulus;
}
else
modulus= high= 0x80000000UL;
if (s_arg)
{
seed= strtol(s_arg, &check, 0);
if (check[0] != '\0')
fatal("bad seed '%s'", s_arg);
srandom(seed);
}
for (i= 0; i<count; i++)
{
do
{
v= random();
} while (v > high);
printf("%lu\n", v % modulus);
}
}
static void fatal(char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s: ", progname);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
static void usage(void)
{
fprintf(stderr, "Usage: rnd [-c <count>] [-m <modulus>] [-s <seed>]\n");
exit(1);
}