From a71cae2d2283c8c59fc851528271213dcc777f64 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 7 Nov 2018 14:46:42 -0300 Subject: [PATCH] add random function --- Makefile | 2 +- src/include/random.h | 4 ++++ src/random.c | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/include/random.h create mode 100644 src/random.c diff --git a/Makefile b/Makefile index 9715102..9f80acf 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-Wall -Werror CPPFLAGS+=-Isrc/include #LDFLAGS=-lm -SRC=src/sort.c +SRC=src/sort.c src/random.c OBJ=$(SRC:.c=.o) all: sort informe diff --git a/src/include/random.h b/src/include/random.h new file mode 100644 index 0000000..c2eabd0 --- /dev/null +++ b/src/include/random.h @@ -0,0 +1,4 @@ +#ifndef _SORT_RANDOM + #define _SORT_RANDOM + int gen_rand(int min, int max); +#endif diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..07b2dac --- /dev/null +++ b/src/random.c @@ -0,0 +1,12 @@ +#include +#include + +static int sort_rand_initialized = 0; + +int gen_rand(int min, int max) { + if (sort_rand_initialized == 0) { + srand((unsigned int) time(NULL)); + sort_rand_initialized = 1; + } + return rand() % (max + 1 - min) + min; +}