xref: /aosp_15_r20/external/ltp/testcases/lib/tst_random.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Red Hat Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Chunyu Hu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
12*49cdfc7eSAndroid Build Coastguard Worker 
print_help(void)13*49cdfc7eSAndroid Build Coastguard Worker static void print_help(void)
14*49cdfc7eSAndroid Build Coastguard Worker {
15*49cdfc7eSAndroid Build Coastguard Worker 	printf("Usage: tst_random <value1> [value2]\n");
16*49cdfc7eSAndroid Build Coastguard Worker 	printf("       Generated random will be between value1 and value2.\n");
17*49cdfc7eSAndroid Build Coastguard Worker 	printf("       If only value1 is specified, value2 will treated as 0.\n");
18*49cdfc7eSAndroid Build Coastguard Worker }
19*49cdfc7eSAndroid Build Coastguard Worker 
get_seed(void)20*49cdfc7eSAndroid Build Coastguard Worker static int get_seed(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval tv;
23*49cdfc7eSAndroid Build Coastguard Worker 	gettimeofday(&tv, NULL);
24*49cdfc7eSAndroid Build Coastguard Worker 	return tv.tv_usec;
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker 
rand_range(long min,long max)27*49cdfc7eSAndroid Build Coastguard Worker static long rand_range(long min, long max)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	return random() % (max - min + 1) + min;
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])32*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	int opt;
35*49cdfc7eSAndroid Build Coastguard Worker 	long min = 0, max = 0;
36*49cdfc7eSAndroid Build Coastguard Worker 	long rval = 0;
37*49cdfc7eSAndroid Build Coastguard Worker 	char *end;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	while ((opt = getopt(argc, argv, ":h")) != -1) {
40*49cdfc7eSAndroid Build Coastguard Worker 		switch (opt) {
41*49cdfc7eSAndroid Build Coastguard Worker 		case 'h':
42*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
43*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
44*49cdfc7eSAndroid Build Coastguard Worker 		default:
45*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
46*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
47*49cdfc7eSAndroid Build Coastguard Worker 		}
48*49cdfc7eSAndroid Build Coastguard Worker 	}
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	if (argc != 2 && argc != 3) {
51*49cdfc7eSAndroid Build Coastguard Worker 		print_help();
52*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
53*49cdfc7eSAndroid Build Coastguard Worker 	}
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	max = strtol(argv[1], &end, 10);
56*49cdfc7eSAndroid Build Coastguard Worker 	if (argv[1][0] == '\0' || *end != '\0') {
57*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid range value1 '%s'\n\n",
58*49cdfc7eSAndroid Build Coastguard Worker 			argv[optind]);
59*49cdfc7eSAndroid Build Coastguard Worker 		print_help();
60*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
61*49cdfc7eSAndroid Build Coastguard Worker 	}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (argc == 3) {
64*49cdfc7eSAndroid Build Coastguard Worker 		min = strtol(argv[2], &end, 10);
65*49cdfc7eSAndroid Build Coastguard Worker 		if (argv[2][0] == '\0' || *end != '\0') {
66*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "ERROR: Invalid range value2 '%s'\n\n",
67*49cdfc7eSAndroid Build Coastguard Worker 				argv[optind+1]);
68*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
69*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
70*49cdfc7eSAndroid Build Coastguard Worker 		}
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	srandom(get_seed());
74*49cdfc7eSAndroid Build Coastguard Worker 	rval = (min > max) ? rand_range(max, min) : rand_range(min, max);
75*49cdfc7eSAndroid Build Coastguard Worker 	printf("%ld\n", rval);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
78*49cdfc7eSAndroid Build Coastguard Worker }
79