Skip to content

random.choice hits ValueError: cannot convert float NaN to integer #58236

Closed

Description

@gpshead
BPO 14028
Nosy @tim-one, @rhettinger, @gpshead, @mdickinson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2012-02-16.17:29:47.660>
created_at = <Date 2012-02-16.07:57:52.837>
labels = ['invalid']
title = 'random.choice hits ValueError: cannot convert float NaN to integer'
updated_at = <Date 2012-02-17.01:23:20.932>
user = 'https://github.com/gpshead'

bugs.python.org fields:

activity = <Date 2012-02-17.01:23:20.932>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2012-02-16.17:29:47.660>
closer = 'gregory.p.smith'
components = []
creation = <Date 2012-02-16.07:57:52.837>
creator = 'gregory.p.smith'
dependencies = []
files = []
hgrepos = []
issue_num = 14028
keywords = []
message_count = 7.0
messages = ['153464', '153473', '153474', '153475', '153476', '153488', '153521']
nosy_count = 4.0
nosy_names = ['tim.peters', 'rhettinger', 'gregory.p.smith', 'mark.dickinson']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue14028'
versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

Activity

  1. gpshead commented on Feb 16, 2012

    @gpshead
    MemberAuthor

    Using a 32-bit Python 2.6.5 on a Linux system at work we observed the following:

    File "/.../lib/python2.6/tempfile.py", line 349, in mktemp
    name = names.next()
    File "/.../lib/python2.6/tempfile.py", line 134, in next
    letters = [choose(c) for dummy in "123456"]
    File "/.../lib/python2.6/random.py", line 261, in choice
    return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
    ValueError: cannot convert float NaN to integer

    This is rare and hard to reproduce. The hardware appears to be healthy and this was on a server with ECC.

    Some searching reveals that other people have hit this in random.choice in Python 2.7 as well: https://bugs.launchpad.net/ubuntu/+source/desktopcouch/+bug/886159

    The ubuntu developer seems to think this is related to time.time() returning NaN at some point (I haven't looked into that myself).

  2. self-assigned this
    on Feb 16, 2012
  3. mdickinson commented on Feb 16, 2012

    @mdickinson
    Member

    Hmm, this is a little odd. For 2.7 at least, the error message is coming from PyLong_FromDouble in Objects/longobject.c. I can't immediately see how PyLong_FromDouble could be called by the random seeding process.

    So it seems more likely that the error is really coming from the int() call in the traceback. But now that implies that the random call is returning NaN, which looks unpossible from the code (random_random in Modules/_randommodule.c).

    static PyObject *
    random_random(RandomObject *self)
    {
        unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
        return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
    }

    So despite your comments about healthy hardware, my bet's on corrupted memory. :-)

  4. mdickinson commented on Feb 16, 2012

    @mdickinson
    Member

    The bugs.launchpad.net URL shows a call to 'entropy.choice'. Any idea what 'entropy' is? Could it be that they're using their own Random subclass, not tied to the Python MT implementation?

  5. rhettinger commented on Feb 16, 2012

    @rhettinger
    Contributor

    The hypothesis that time.time() is returning NaN doesn't match the provided traceback. If time.time() had returned NaN, the exception would have happened earlier, on line 113 in random.py: long(time.time() * 256)

    I'm wondering if the NaN arises in the C code for random():

    random_random(RandomObject *self)
    {
        unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
        return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
    }

    Upstream from that, only integers are used, so this would be the earliest a NaN could arise when running the code in choice(): return seq[int(self.random() * len(seq))]

  6. mdickinson commented on Feb 16, 2012

    @mdickinson
    Member

    I'm wondering if the NaN arises in the C code for random():

    I don't think that's possible. In the second line:

    return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
    

    a and b are already C unsigned longs, so no matter what their value, the result of the expression is well in range for an IEEE 754 double, and on a normal machine there's just no realistic way that this calculation could produce a NaN. PyFloat_FromDouble does no manipulation of the C double, but just stores it directly in the PyFloat object.

    I think there are two different things going on here.

    (1) The Ubuntu error reporter seems to be using something other than the standard Random class, so all bets are off there without knowing more about what's being used. Chances seem good that whatever random number generator they're using really *is* producing a NaN.

    (2) That leaves Greg's report above, where the standard Random class is apparently what's being used. Here I'm baffled---I can't see any realistic mechanism that might produce that traceback.

  7. gpshead commented on Feb 16, 2012

    @gpshead
    MemberAuthor

    I think my claim the hardware appears healthy was premature. I misunderstood our initial error report internally on where the code ran and was looking at the wrong host. doh. my bad.

    Several more of these have been found in the last week and they all suspiciously ran on the same machine. One of them had a _different_ failure that is an even stronger suggestion of bad hardware:

    File "/.../lib/python2.6/random.py", line 57, in <module>
    NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
    ValueError: math domain error

    Sorry for the false alarm.

  8. rhettinger commented on Feb 17, 2012

    @rhettinger
    Contributor

    Well, at least it was an interesting bug report ;-)

  9. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions