Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ implementation for Python.
Installation
============

A standard setup.py is provided.
.. code-block:: bash

$ pip install java-random

Usage
=====

.. code-block:: python

>>> import javarandom
>>> r = javarandom.Random(0)
>>> r.nextDouble()
0.730967787376657

FAQ
====
Expand Down
15 changes: 8 additions & 7 deletions javarandom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import math
import time


class Random(object):
"""
Expand All @@ -16,7 +17,7 @@ class Random(object):
synchronize all accesses to this class per-instance.
"""

def __init__(self, seed = None):
def __init__(self, seed=None):
"""
Create a new random number generator.
"""
Expand All @@ -40,7 +41,7 @@ def seed(self):

@seed.setter
def seed(self, seed):
self._seed = (seed ^ 0x5deece66d) & ((1 << 48) - 1)
self._seed = (seed ^ 0x5DEECE66D) & ((1 << 48) - 1)

def next(self, bits):
"""
Expand All @@ -56,13 +57,13 @@ def next(self, bits):
elif bits > 32:
bits = 32

self._seed = (self._seed * 0x5deece66d + 0xb) & ((1 << 48) - 1)
self._seed = (self._seed * 0x5DEECE66D + 0xB) & ((1 << 48) - 1)
retval = self._seed >> (48 - bits)

# Python and Java don't really agree on how ints work. This converts
# the unsigned generated int into a signed int if necessary.
if retval & (1 << 31):
retval -= (1 << 32)
retval -= 1 << 32

return retval

Expand All @@ -74,14 +75,14 @@ def nextBytes(self, l):
for i in range(0, len(l)):
if not i % 4:
n = self.nextInt()
b = n & 0xff
b = n & 0xFF
# Flip signs. Ugh.
if b & 0x80:
b -= 0x100
l[i] = b
n >>= 8

def nextInt(self, n = None):
def nextInt(self, n=None):
"""
Return a random int in [0, `n`).

Expand Down
20 changes: 9 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
from distutils.core import setup

setup(
name="java-random",
version="1.0",
description="Implementation of Java's Random",
long_description=open("README.rst").read(),
author="Corbin Simpson",
author_email="MostAwesomeDude@gmail.com",
url="http://github.com/MostAwesomeDude/java-random",
py_modules=[
"javarandom",
],
)
name="java-random",
version="1.0",
description="Implementation of Java's Random",
long_description=open("README.rst").read(),
author="Corbin Simpson",
author_email="MostAwesomeDude@gmail.com",
url="http://github.com/MostAwesomeDude/java-random",
py_modules=["javarandom"],
)
6 changes: 3 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import javarandom

class AccuracyTest(unittest.TestCase):

class AccuracyTest(unittest.TestCase):
def setUp(self):
self.r = javarandom.Random(0)

Expand Down Expand Up @@ -44,8 +44,8 @@ def test_nextDouble(self):

def test_nextGaussian(self):
standard = 0.80253306373903050, -0.90154608841751220
self.assertEqual((self.r.nextGaussian(), self.r.nextGaussian()),
standard)
self.assertEqual((self.r.nextGaussian(), self.r.nextGaussian()), standard)


if __name__ == "__main__":
unittest.main()