Add mpw.rndm to return random in range 1 < r < b-1.
authorjbj <devnull@localhost>
Wed, 30 Apr 2003 21:35:20 +0000 (21:35 +0000)
committerjbj <devnull@localhost>
Wed, 30 Apr 2003 21:35:20 +0000 (21:35 +0000)
CVS patchset: 6799
CVS date: 2003/04/30 21:35:20

python/rng.py [new file with mode: 0644]
python/rpmmpw-py.c
python/rpmrng-py.c
python/rpmrng-py.h

diff --git a/python/rng.py b/python/rng.py
new file mode 100644 (file)
index 0000000..182f799
--- /dev/null
@@ -0,0 +1,7 @@
+import rpm
+
+rpm.rng().Debug(-1);
+r = rpm.rng()
+m = rpm.mpw(10)
+
+print m.rndm(m,r)
index 93fe29420d1370378646f49afe07aced298714b1..f7d10fee92c3008874c39a9dc3a502ebaae28803 100644 (file)
@@ -11,6 +11,7 @@
 #endif
 
 #include "rpmmpw-py.h"
+#include "rpmrng-py.h"
 
 #include "rpmdebug-py.c"
 
@@ -1182,6 +1183,13 @@ fprintf(stderr, "    b %p[%d]:\t", m->n.data, m->n.size), mpprintln(stderr, m->n
        mpnsize(&z->n, m->n.size);
        mpbinv_w(&b, x->n.size, x->n.data, z->n.data, wksp);
        break;
+    case 'R':
+    {  rngObject * r = ((rngObject *)x);
+       wksp = alloca(m->n.size*sizeof(*wksp));
+       mpbset(&b, m->n.size, m->n.data);
+       mpnsize(&z->n, m->n.size);
+       mpbrnd_w(&b, &r->rngc, z->n.data, wksp);
+    }  break;
     case 'S':
        wksp = alloca((4*m->n.size+2)*sizeof(*wksp));
        mpbset(&b, m->n.size, m->n.data);
@@ -1382,6 +1390,23 @@ mpw_Powm(mpwObject * s, PyObject * args)
                mpw_i2mpw(xo), mpw_i2mpw(yo), mpw_i2mpw(mo));
 }
 
+/** \ingroup py_c
+ * Return random number 1 < r < b-1.
+ */
+static PyObject *
+mpw_Rndm(mpwObject * s, PyObject * args)
+        /*@*/
+{
+    PyObject * xo, * mo;
+
+    if (!PyArg_ParseTuple(args, "OO:Rndm", &mo, &xo)) return NULL;
+    if (!is_rng(xo)) {
+       PyErr_SetString(PyExc_TypeError, "mpw.rndm() requires rng_Type argument");
+       return NULL;
+    }
+    return mpw_ops2("Rndm", 'R', (mpwObject*)xo, mpw_i2mpw(mo));
+}
+
 /*@-fullinitblock@*/
 /*@unchecked@*/ /*@observer@*/
 static struct PyMethodDef mpw_methods[] = {
@@ -1401,6 +1426,8 @@ static struct PyMethodDef mpw_methods[] = {
        NULL},
  {"powm",      (PyCFunction)mpw_Powm,  METH_VARARGS,
        NULL},
+ {"rndm",      (PyCFunction)mpw_Rndm,  METH_VARARGS,
+       NULL},
  {NULL,                NULL}           /* sentinel */
 };
 /*@=fullinitblock@*/
index 945d4a4cf68726c6b7b5a2bf9afc11d72bf9854e..5671c31fec9e91adfbe248501f987763dee77e09 100644 (file)
@@ -19,8 +19,6 @@
 /*@unchecked@*/
 static int _rng_debug = 0;
 
-#define is_rng(o)      ((o)->ob_type == &rng_Type)
-
 /*@unchecked@*/ /*@observer@*/
 static const char initialiser_name[] = "rpm.rng";
 
index 3c6c3fa36e6e0afa8fda46e15134885cf1f920fa..5f1b5290297cf766462fbb194579ced811bf8ddf 100644 (file)
@@ -19,5 +19,6 @@ typedef struct rngObject_s {
  */
 /*@unchecked@*/
 extern PyTypeObject rng_Type;
+#define is_rng(o)      ((o)->ob_type == &rng_Type)
 
 #endif