Statistiques
| Révision :

root / Pi / C / CAPI / pimodule.c @ 85

Historique | Voir | Annoter | Télécharger (1,23 ko)

1
#include <Python.h>
2
#include <math.h>
3

    
4
// Marsaglia RNG very simple implementation
5
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
6
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
7
#define MWC   (znew+wnew)
8
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
9
#define CONG  (jcong=69069*jcong+1234567)
10
#define KISS  ((MWC^CONG)+SHR3)
11

    
12
#define MWCfp MWC * 2.328306435454494e-10f
13
#define KISSfp KISS * 2.328306435454494e-10f
14

    
15
int _InsideCircle(unsigned int iterations,
16
                  unsigned int seed_w,unsigned int seed_z)
17
{
18
   unsigned int z=seed_z;
19
   unsigned int w=seed_w;
20
   unsigned int i;
21

    
22
   int total=0;
23

    
24
   for (i=0;i<iterations;i++) {
25

    
26
      float x=MWCfp ;
27
      float y=MWCfp ;
28

    
29
      // Matching test
30
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
31
      total+=inside;
32
   }
33

    
34
   return(total);
35
}
36

    
37
static PyObject* InsideCircle(PyObject* self, PyObject* args)
38
{
39
  int i,z,w;
40
 
41
  if (!PyArg_ParseTuple(args, "iii", &i,&z,&w))
42
    return NULL;
43
 
44
  return Py_BuildValue("i", _InsideCircle(i,z,w));
45
}
46
 
47
static PyMethodDef MonteCarloMethods[] = {
48
  {"InsideCircle", InsideCircle, METH_VARARGS, "Inside Circle Randomly Check"},
49
  {NULL, NULL, 0, NULL}
50
};
51
 
52
PyMODINIT_FUNC
53
initMonteCarlo(void)
54
{
55
  (void) Py_InitModule("MonteCarlo", MonteCarloMethods);
56
}