Git Product home page Git Product logo

Comments (8)

lemire avatar lemire commented on July 28, 2024

I implemented the change, and it is faster...

Before...

python -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 3: 0.0924 usec per loop

After...

python -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 3: 0.0602 usec per loop

My code differs slightly from yours, but should be equivalent.

Thanks.

from fastrand.

achan001 avatar achan001 commented on July 28, 2024

You welcome.

Some bad output and error messages with your patch.

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from fastrand import *        # PyLong_AsUnsignedLong PATCH
>>> print pcg32bounded(0)         # should not return anything
0
>>> pcg32bounded(-1)              # error message mysteriously delayed
41705474
>>>
OverflowError: can't convert negative value to unsigned long
>>>
>>> pcg32bounded('hi')            # Python 2 SystemError bug ?
-888685582
>>>
SystemError: ..\Objects\longobject.c:336: bad argument to internal function
>>>

My PyLong_AsLong patch may be better. It handled all the error message as expected.
(and it match the description of pcg32bounded doc-string)

from fastrand.

lemire avatar lemire commented on July 28, 2024

@achan001

Can you verify your exact patch on modern Pythons? Python 2.7 and 3.6?

I do not get at all what you are getting with your patch. And I don't think it makes sense to install an old Python.

from fastrand.

achan001 avatar achan001 commented on July 28, 2024

My patch verified OK with Python latest 3.6.5 (Windows x86 executive installer)

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from fastrand import *
>>> print(pcg32bounded(1001))
82
>>> print(pcg32bounded(0))
None
>>> print(pcg32bounded(2**31-1), pcg32bounded(-2**31))
20852737 None
>>> print(pcg32bounded(2**31))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
>>>
>>> print(pcg32bounded('Hi'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

Perhaps you are using 64-bits system ?
if (n <= 0) test may need some adjustment ...

What unexpected output are you getting ?

from fastrand.

lemire avatar lemire commented on July 28, 2024

I merged your code (see GitHub). I used to get an error when inputting a negative value, now I just get nothing. It does not sound like progress to me.

See:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastrand
>>> fastrand.pcg32bounded(-1)
>>> fastrand.pcg32bounded(0)

from fastrand.

lemire avatar lemire commented on July 28, 2024

I would much rather get a SystemError than nothing when a negative value is used.

from fastrand.

achan001 avatar achan001 commented on July 28, 2024

You mean like this ? (I picked ValueError, for SystemError, use PyExc_SystemError)

>>> import fastrand
>>> fastrand.pcg32bounded(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: no such random number exist
>>>
>>> fastrand.pcg32bounded(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: no such random number exist
>>>

This is the patch:

#if PY_MAJOR_VERSION >= 3
#define PyInt_AsLong(x)   PyLong_AsLong(x)
#endif

static PyObject*
pcg32bounded(PyObject* self, PyObject* args)
{
    int n = PyInt_AsLong(args);
    if (n > 0)
      return Py_BuildValue("i", pcg32_random_bounded_divisionless(n));
    if (!PyErr_Occurred())
      PyErr_SetString(PyExc_ValueError, "no such random number exist");
    return NULL;
}

from fastrand.

achan001 avatar achan001 commented on July 28, 2024

You are correct.
Raising an exception is better than return None, warning problem immediately.

This is python gmpy2 behavior for non-exist values.

>>> gmpy2.invert(101, 12345)
mpz(9656)
>>>
>>> gmpy2.invert(105, 12345)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: invert() no inverse exists

from fastrand.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.