diff options
| author | Jacob McDonnell <jacob@simplelittledream.com> | 2021-08-18 17:31:19 -0400 |
|---|---|---|
| committer | Jacob McDonnell <jacob@simplelittledream.com> | 2021-08-18 17:31:19 -0400 |
| commit | 9a6959b2700ec54392fb2ffb145bacae66fc2269 (patch) | |
| tree | 34704cfbbb55e349ab91c09138911f2906e90988 /rng/src | |
initial commit
Diffstat (limited to 'rng/src')
| -rw-r--r-- | rng/src/rng.c | 52 |
1 files changed, 52 insertions, 0 deletions
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 <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; +} |
