blob: 375e0adadd5838a72b0c23d826eaf352558c4813 (
plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include <limits.h>
#include <stdlib.h>
#include "index.h"
#if (RAND_MAX < UCHAR_MAX)
#error "RAND_MAX < UCHAR_MAX, unable to generate random numbers."
#endif
#if (RAND_MAX == 0)
#error "RAND_MAX is included in [0, 0]. What are you even doing?"
#endif
/*
* Returns a random unsigned char.
*/
static unsigned char random_uchar (void)
{
return
(unsigned char)
(
/* FIXME: Do floats allow enough precision for this? */
(
((float) rand())
/ ((float) RAND_MAX)
)
* ((float) UCHAR_MAX)
);
}
/* See: "index.h" */
ZoO_index ZoO_index_random (void)
{
ZoO_index i;
ZoO_index result;
unsigned char * result_bytes;
result_bytes = (unsigned char *) &result;
for (i = 0; i < sizeof(ZoO_index); ++i)
{
result_bytes[i] = random_uchar();
}
return result;
}
/* See: "index.h" */
ZoO_index ZoO_index_random_up_to (const ZoO_index max)
{
return
(ZoO_index)
(
/* FIXME: Do floats allow enough precision for this? */
(
((float) ZoO_index_random())
/ ((float) ZoO_INDEX_MAX)
)
* ((float) max)
);
}
|