-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_frand.c
37 lines (34 loc) · 1.2 KB
/
ft_frand.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
33
34
35
36
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_frand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smonroe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/21 07:42:48 by smonroe #+# #+# */
/* Updated: 2018/04/21 08:50:02 by smonroe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <fcntl.h>
float ft_frand(void)
{
char num[9];
int n;
int i;
int r;
float f;
i = 0;
r = 0;
f = 0.0f;
n = open("/dev/random", O_RDONLY);
read(n, num, 9);
close(n);
while (i < 9)
r += r * 10 + (num[i++] - '0');
r %= 2147483647;
if (r < 0)
r = -r;
f += (r / 2147483647.0f);
return (f);
}