-
Notifications
You must be signed in to change notification settings - Fork 0
/
isaac.c
101 lines (79 loc) · 2.9 KB
/
isaac.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Copyright 2011 Douglas Bagnall <douglas@paradise.net.nz> MIT License
*
* Part of Riffle, a collection of random number generators
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* The Software is provided "as is", WITHOUT WARRANTY of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. in no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the software or the use or other dealings in
* the Software.
*/
/* Based on Python 3.1's original randommodule.c, which was in turn based on
Takuji Nishimura and Makoto Matsumoto's MT19937 code. However, the parts
of randommodule.c that remain in this file are almost certainly not from
Nishimura and Matsumoto, rather they are the adaptations and boilerplate to
match Python's Random() interface, and were seemingly mostly written by
Raymond Hettinger in 2002.
*/
#include "Python.h"
#include "random_helpers.h"
#include "ccan/isaac/isaac.h"
#define MODULE_NAME isaac
#ifndef KEY_BYTES
#define KEY_BYTES (160 / 8)
#endif
#ifndef IV_BYTES
#define IV_BYTES (0 / 8)
#endif
#define BUFFER_DOUBLES (16)
typedef struct {
PyObject_HEAD
struct isaac_ctx context;
double numbers[BUFFER_DOUBLES];
u32 index;
} RandomObject;
static PyTypeObject Random_Type;
#define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
/* Random methods */
/* random_random return a double in the range [0, 1).
*/
static PyObject *
random_random(RandomObject *self)
{
double d = isaac_next_double(&self->context);
return PyFloat_FromDouble(d);
}
static PyObject *
random_seed(RandomObject *self, PyObject *args)
{
PyObject *arg = NULL;
u8 seed[KEY_BYTES + IV_BYTES + 4];
if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
return NULL;
if (extract_seed(arg, seed, sizeof(seed)) != 0){
return NULL;
}
isaac_init(&self->context, seed, sizeof(seed));
Py_INCREF(Py_None);
return Py_None;
}
RANDOM_DUMMY_STATE_SETTERS()
RANDOM_CLASS_NEW()
RANDOM_METHODS_STRUCT_NO_GETRANDBITS();
RANDOM_CLASS_DOC(MODULE_NAME);
RANDOM_OBJECT_STRUCT(MODULE_NAME);
RANDOM_MODULE_DOC(MODULE_NAME);
RANDOM_MODULE_STRUCT(MODULE_NAME);
RANDOM_MODULE_INIT2(MODULE_NAME)