4 Generate random numbers
15 static char *progname
;
17 static void fatal(char *fmt
, ...);
18 static void usage(void);
20 int main(int argc
, char *argv
[])
23 unsigned long n
, v
, high
, modulus
;
26 char *c_arg
, *m_arg
, *s_arg
;
28 (progname
=strrchr(argv
[0],'/')) ? progname
++ : (progname
=argv
[0]);
30 c_arg
= m_arg
= s_arg
= NULL
;
31 while (c
= getopt(argc
, argv
, "?c:m:s:"), c
!= -1)
35 case 'c': c_arg
= optarg
; break;
36 case 'm': m_arg
= optarg
; break;
37 case 's': s_arg
= optarg
; break;
39 fatal("getopt failed: '%c'", c
);
46 count
= strtol(c_arg
, &check
, 0);
48 fatal("bad count '%s'", c_arg
);
54 modulus
= strtoul(m_arg
, &check
, 0);
55 if (check
[0] != '\0' || modulus
== 0)
56 fatal("bad modulus '%s'", m_arg
);
57 n
= 0x80000000UL
/ modulus
;
59 fatal("bad modulus %lu (too big)", modulus
);
63 modulus
= high
= 0x80000000UL
;
66 seed
= strtol(s_arg
, &check
, 0);
68 fatal("bad seed '%s'", s_arg
);
72 for (i
= 0; i
<count
; i
++)
79 printf("%lu\n", v
% modulus
);
83 static void fatal(char *fmt
, ...)
87 fprintf(stderr
, "%s: ", progname
);
89 vfprintf(stderr
, fmt
, ap
);
91 fprintf(stderr
, "\n");
95 static void usage(void)
97 fprintf(stderr
, "Usage: rnd [-c <count>] [-m <modulus>] [-s <seed>]\n");