summaryrefslogtreecommitdiff
path: root/rng/src/rng.c
blob: 4243268e272168c3812eed54dcaebb89556f173f (plain)
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
#include <tice.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define EnterKey 5
#define maxline 22
#define ExitKey 9
#define ExitCode -1

int rng(int min, int max);
int getInput(void);

int main(void)
{
	while (true) {
		os_ClrHome();

		int min, max;
		printf("Random Number\nPress Clear to exit\nMinimun: ");
		if ((min = getInput()) == ExitCode)
			break;
		printf("\nMaximum: ");
		if ((max = getInput()) == ExitCode)
			break;
		printf("\n%d", rng(min, max));

		while (!os_GetCSC());
	}
}

int getInput(void)
{
	char number[maxline];
	char *ptr;
	int16_t key, i = 0, output;
	while((key = os_GetKey()) != EnterKey) {
		if(key == ExitKey)
			return ExitCode;
		number[i] = (key - 142) + '0';
		printf("%c", number[i++]);
	}
	output = strtol(number, &ptr, 10);
	return output;
}

int rng(int min, int max)
{
	time_t t;
	srand((unsigned) time(&t));
	return (rand() % (max - min)) + min;
}