Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

random.randint() errors if second value is greater than 2000000 #33

Open
pyjsorg opened this Issue · 1 comment

1 participant

@pyjsorg
Owner

Before reporting a problem, please UPDATE TO LATEST SVN.
As this is a community-driven project, the developers only
have time available to fix problems reported in the LATEST
code. You are welcome to report problems in any other
version, but until you have upgraded to latest svn and provided
a test report against that, you will be reliant on people
OTHER than the core developers for bug-fixes. which could
happen, so feel free to ignore the above should you so choose.

What steps will reproduce the problem?

import random
random.randint(0,3000000)

What is the expected output? What do you see instead?

Expect a random number within range. Get a very unhelpful alert saying "typeError: Object1 doesnt have 'lshift' "

What version of Pyjamas are you using? which svn revision number?
On what operating system?

latest pyjs as of feb 23 2012. Windows 7 starter. latest chrome.

If using PyJS, have you attached a javascript stack trace,
from IE's Script Debugger or from Firefox Venkman / Firebug?
Have you attached a stack trace from using "-d" compiler option?

no stack trace, only an alert.

If using PyJD, have you attached a python stack trace?

If using PyJS, what compiler options did you use?

-c -d

Please provide any additional information below.

probably caused by this line in random.py:

def randrange(self, start, stop=None, step=1, fint=int, default=None, maxwidth=1L<<BPF):

Original issue: http://code.google.com/p/pyjamas/issues/detail?id=690 (February 23, 2012 12:47:10)

@pyjsorg
Owner

From cornelis...@gmail.com on February 24, 2012 07:10:06:
As you noted email: if the second value is above somewhere between 2 and 3 million you get a very unhelpful lshift error message when loading the app in pyjs.

It should work with the --strict translator option (or the option that forces the use of python integers in stead of javascript 'integers'). Javascript doesn't have real integers (only floats).

If it is the 1<<BPF, then the following patch should work. The 1<<BPF is replaced by the value of 1<<BPF. Could you check this?

{{{

diff --git a/pyjs/src/pyjs/lib/random.py b/pyjs/src/pyjs/lib/random.py
index 1cea795..dbdd8f8 100644
--- a/pyjs/src/pyjs/lib/random.py
+++ b/pyjs/src/pyjs/lib/random.py
@@ -21,7 +21,7 @@ LOG4 = _log(4.0)
SG_MAGICCONST = 1.0 + _log(4.5)
BPF = 53 # Number of bits in a float

RECIP_BPF = 2**-BPF

+BPF_VALUE = 9007199254740992L # 1 << BPF

# Translated by Guido van Rossum from C source provided by
# Adrian Baddeley. Adapted by Raymond Hettinger for use with
@@ -96,7 +96,7 @@ class Random(_random.Random):
## -------------------- integer methods -------------------

 def randrange(self, start, stop=None, step=1, fint=int, default=None,
  • maxwidth=1L<<BPF):
  •              maxwidth=BPF_VALUE):
     # """Choose a random item from range(start, stop[, step]).
     # This fixes the problem with randint() which includes the
     # endpoint; in Python this is usually not what you want.
    

    @@ -164,7 +164,7 @@ class Random(_random.Random):

     return self.randrange(a, b+1)
    
  • def _randbelow(self, n, _log=_log, fint=int, _maxwidth=1L<<BPF):

  • def _randbelow(self, n, _log=_log, fint=int, _maxwidth=BPF_VALUE): #def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF, # _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType): # """Return a random int in the range [0,n)

}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.