Remove building with NOCRYPTO option
[minix.git] / minix / commands / swifi / tests / rnd.c
blob41a7fac8c931ca8170dc793c3e3d3a5f4a7f7680
1 /*
2 rnd.c
4 Generate random numbers
5 */
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 static char *progname;
15 static void fatal(char *fmt, ...);
16 static void usage(void);
18 int main(int argc, char *argv[])
20 int c, i, count;
21 unsigned long n, v, high, modulus;
22 unsigned seed;
23 char *check;
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)
31 switch(c)
33 case 'c': c_arg= optarg; break;
34 case 'm': m_arg= optarg; break;
35 case 's': s_arg= optarg; break;
36 default:
37 fatal("getopt failed: '%c'", c);
40 if (optind != argc)
41 usage();
42 if (c_arg)
44 count= strtol(c_arg, &check, 0);
45 if (check[0] != '\0')
46 fatal("bad count '%s'", c_arg);
48 else
49 count= 1;
50 if (m_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;
56 if (n == 0)
57 fatal("bad modulus %lu (too big)", modulus);
58 high= n * modulus;
60 else
61 modulus= high= 0x80000000UL;
62 if (s_arg)
64 seed= strtol(s_arg, &check, 0);
65 if (check[0] != '\0')
66 fatal("bad seed '%s'", s_arg);
67 srandom(seed);
70 for (i= 0; i<count; i++)
74 v= random();
75 } while (v > high);
77 printf("%lu\n", v % modulus);
81 static void fatal(char *fmt, ...)
83 va_list ap;
85 fprintf(stderr, "%s: ", progname);
86 va_start(ap, fmt);
87 vfprintf(stderr, fmt, ap);
88 va_end(ap);
89 fprintf(stderr, "\n");
90 exit(1);
93 static void usage(void)
95 fprintf(stderr, "Usage: rnd [-c <count>] [-m <modulus>] [-s <seed>]\n");
96 exit(1);