From 9a6959b2700ec54392fb2ffb145bacae66fc2269 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 18 Aug 2021 17:31:19 -0400 Subject: initial commit --- rng/src/rng.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 rng/src/rng.c (limited to 'rng/src/rng.c') diff --git a/rng/src/rng.c b/rng/src/rng.c new file mode 100644 index 0000000..4243268 --- /dev/null +++ b/rng/src/rng.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3