-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_rand.c
32 lines (29 loc) · 1.14 KB
/
ft_rand.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smonroe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/21 00:01:05 by smonroe #+# #+# */
/* Updated: 2018/04/21 08:50:08 by smonroe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <fcntl.h>
int ft_rand(int max)
{
char num[9];
int n;
int i;
int r;
i = 0;
r = 0;
n = open("/dev/random", O_RDONLY);
read(n, num, 9);
close(n);
while (i < 9)
r += r * 10 + (num[i++] - '0');
r %= max;
return (r < 0 ? -r : r);
}