42_cube3D/libftx/extra/ft_random_generator.c

39 lines
1.3 KiB
C
Raw Normal View History

2023-04-26 07:02:44 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_random_generator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 18:43:27 by cchauvet #+# #+# */
/* Updated: 2023/01/09 20:09:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "extra.h"
size_t ft_random_generator(size_t start, size_t stop)
{
int fd;
char *line;
int sum;
size_t i;
fd = open("/dev/random", O_RDONLY);
if (fd == -1)
return (0);
line = ft_calloc(sizeof(char), 10001);
if (line == NULL)
return (0);
read(fd, line, 10000);
sum = 0;
i = 0;
while (i < ft_strlen(line))
{
sum += (unsigned int) line[i];
i++;
}
free(line);
return (sum % (stop - start));
}