4 Generate random numbers
13 static char *progname
;
15 static void fatal(char *fmt
, ...);
16 static void usage(void);
18 int main(int argc
, char *argv
[])
21 unsigned long n
, v
, high
, modulus
;
24 char *c_arg
, *m_arg
, *s_arg
;
26 (progname
=strrchr(argv
[0],'/')) ? progname
++ : (progname
=argv
[0]);
28 c_arg
= m_arg
= s_arg
= NULL
;
29 while (c
= getopt(argc
, argv
, "?c:m:s:"), c
!= -1)
33 case 'c': c_arg
= optarg
; break;
34 case 'm': m_arg
= optarg
; break;
35 case 's': s_arg
= optarg
; break;
37 fatal("getopt failed: '%c'", c
);
44 count
= strtol(c_arg
, &check
, 0);
46 fatal("bad count '%s'", c_arg
);
52 modulus
= strtoul(m_arg
, &check
, 0);
53 if (check
[0] != '\0' || modulus
== 0)
54 fatal("bad modulus '%s'", m_arg
);
55 n
= 0x80000000UL
/ modulus
;
57 fatal("bad modulus %lu (too big)", modulus
);
61 modulus
= high
= 0x80000000UL
;
64 seed
= strtol(s_arg
, &check
, 0);
66 fatal("bad seed '%s'", s_arg
);
70 for (i
= 0; i
<count
; i
++)
77 printf("%lu\n", v
% modulus
);
81 static void fatal(char *fmt
, ...)
85 fprintf(stderr
, "%s: ", progname
);
87 vfprintf(stderr
, fmt
, ap
);
89 fprintf(stderr
, "\n");
93 static void usage(void)
95 fprintf(stderr
, "Usage: rnd [-c <count>] [-m <modulus>] [-s <seed>]\n");