Skip to content

Instantly share code, notes, and snippets.

@cogmission
Last active September 21, 2016 15:54
Show Gist options
  • Save cogmission/c4cb8feaba19595dae8ff964e18b05d0 to your computer and use it in GitHub Desktop.
Save cogmission/c4cb8feaba19595dae8ff964e18b05d0 to your computer and use it in GitHub Desktop.

Revisions

  1. cogmission revised this gist Sep 21, 2016. 2 changed files with 50 additions and 5 deletions.
    37 changes: 33 additions & 4 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -112,10 +112,29 @@ public int[] sample(TIntArrayList choices, int[] selectedIndices) {
    upperBound--;
    }
    Arrays.sort(selectedIndices);
    System.out.println("sample: " + Arrays.toString(selectedIndices));
    //System.out.println("sample: " + Arrays.toString(selectedIndices));
    return selectedIndices;
    }

    /**
    * Fisher-Yates implementation which shuffles the array contents.
    *
    * @param array the array of ints to shuffle.
    * @return shuffled array
    */
    public int[] shuffle(int[] array) {
    int index;
    for (int i = array.length - 1; i > 0; i--) {
    index = nextInt(i + 1);
    if (index != i) {
    array[index] ^= array[i];
    array[i] ^= array[index];
    array[index] ^= array[i];
    }
    }
    return array;
    }

    /**
    * Returns an array of floating point values of the specified shape
    *
    @@ -190,14 +209,14 @@ public int[][] binDistrib(int rows, int cols, double sparsity) {
    public double nextDouble() {
    int nd = nextInt(10000);
    double retVal = new BigDecimal(nd * .0001d, MATH_CONTEXT).doubleValue();
    System.out.println("nextDouble: " + retVal);
    //System.out.println("nextDouble: " + retVal);
    return retVal;
    }

    @Override
    public int nextInt() {
    int retVal = nextInt(Integer.MAX_VALUE);
    System.out.println("nextIntNB: " + retVal);
    //System.out.println("nextIntNB: " + retVal);
    return retVal;
    }

    @@ -221,7 +240,7 @@ public int nextInt(int bound) {
    ;
    */
    }
    System.out.println("nextInt(" + bound + "): " + r);
    //System.out.println("nextInt(" + bound + "): " + r);
    return r;
    }

    @@ -338,6 +357,16 @@ public static void main(String[] args) {
    random.sampleWithPrintout(choices, selectedIndices, collectedRandoms);
    System.out.println("samples are equal ? " + Arrays.equals(expectedSample, selectedIndices));
    System.out.println("used randoms are equal ? " + collectedRandoms.equals(expectedRandoms));

    random = new UniversalRandom(42);
    int[] coll = ArrayUtils.range(0, 10);
    int[] before = Arrays.copyOf(coll, coll.length);
    random.shuffle(coll);
    System.out.println("collection before: " + Arrays.toString(before));
    System.out.println("collection shuffled: " + Arrays.toString(coll));
    int[] expected = { 5, 1, 8, 6, 2, 4, 7, 3, 9, 0 };
    System.out.println(Arrays.equals(expected, coll));
    System.out.println(!Arrays.equals(expected, before)); // not equal
    }

    }
    18 changes: 17 additions & 1 deletion universal_random.py
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ def getSeed(self):
    return self.seed

    def rshift(self, val, n):
    #return val>>n if val >= 0 else (val+0x100000000)>>n
    if val >= 0:
    return val>>n
    else:
    @@ -81,10 +82,23 @@ def sample(self, choices, selectedIndices):
    print "sample: " + str(list(selectedIndices))
    return selectedIndices;

    def shuffle(self, collection):
    """
    Modeled after Fisher - Yates implementation
    """
    index = None
    for i in range(len(collection) - 1, 0, -1):
    index = self.nextInt(i + 1)
    if index != i:
    collection[index] ^= collection[i]
    collection[i] ^= collection[index]
    collection[index] ^= collection[i]

    return collection

    def rand(self, rows, cols):
    """
    Returns a list of binary values of the specified shape
    Returns an array of floating point values of the specified shape
    @param rows (int) the number of rows
    @param cols (int) the number of columns
    @@ -132,6 +146,7 @@ def bin_distrib(self, rows, cols, sparsity):
    item = to_fill[ind]
    to_fill = numpy.delete(to_fill, ind)
    retval[i][item] = sparsity
    print "retval = " + str(list(retval[i]))
    cnt -= 1
    elif sublen > target:
    cnt = sublen
    @@ -140,6 +155,7 @@ def bin_distrib(self, rows, cols, sparsity):
    item = sub[ind]
    sub = numpy.delete(sub, ind)
    retval[i][item] = 0.0
    print "retval = " + str(list(retval[i]))
    cnt -= 1

    retval = (retval >= sparsity).astype(uintType)
  2. cogmission revised this gist Sep 14, 2016. 2 changed files with 222 additions and 14 deletions.
    147 changes: 142 additions & 5 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,24 @@
    /* ---------------------------------------------------------------------
    * Numenta Platform for Intelligent Computing (NuPIC)
    * Copyright (C) 2016, Numenta, Inc. Unless you have an agreement
    * with Numenta, Inc., for a separate license for this software code, the
    * following terms and conditions apply:
    *
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU Affero Public License version 3 as
    * published by the Free Software Foundation.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    * See the GNU Affero Public License for more details.
    *
    * You should have received a copy of the GNU Affero Public License
    * along with this program. If not, see http://www.gnu.org/licenses.
    *
    * http://numenta.org/licenses/
    * ---------------------------------------------------------------------
    */
    package org.numenta.nupic.util;

    import java.math.BigDecimal;
    @@ -8,9 +29,22 @@
    import java.util.List;
    import java.util.Random;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;

    import gnu.trove.list.array.TIntArrayList;
    import gnu.trove.set.hash.TIntHashSet;

    /**
    * <p>
    * This also has a Python version which is guaranteed to output the same random
    * numbers if given the same initial seed value.
    * </p><p>
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random.
    * <p>
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml.
    * @author cogmission
    */
    public class UniversalRandom extends Random {
    /** serial version */
    private static final long serialVersionUID = 1L;
    @@ -44,12 +78,15 @@ public long getSeed() {
    return seed;
    }

    /*
    * Internal method used for testing
    */
    private int[] sampleWithPrintout(TIntArrayList choices, int[] selectedIndices, List<Integer> collectedRandoms) {
    TIntArrayList choiceSupply = new TIntArrayList(choices);
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);

    //System.out.println("randomIdx: " + randomIdx);
    collectedRandoms.add(randomIdx);
    selectedIndices[i] = (choiceSupply.removeAt(randomIdx));
    upperBound--;
    @@ -75,22 +112,92 @@ public int[] sample(TIntArrayList choices, int[] selectedIndices) {
    upperBound--;
    }
    Arrays.sort(selectedIndices);

    System.out.println("sample: " + Arrays.toString(selectedIndices));
    return selectedIndices;
    }

    /**
    * Returns an array of floating point values of the specified shape
    *
    * @param rows the number of rows
    * @param cols the number of cols
    * @return
    */
    public double[][] rand(int rows, int cols) {
    double[][] retval = new double[rows][cols];
    for(int i = 0;i < rows;i++) {
    for(int j = 0;j < cols;j++) {
    retval[i][j] = nextDouble();
    }
    }
    return retval;
    }

    /**
    * Returns an array of binary values of the specified shape whose
    * total number of "1's" will reflect the sparsity specified.
    *
    * @param rows the number of rows
    * @param cols the number of cols
    * @param sparsity number between 0 and 1, indicating percentage
    * of "on" bits
    * @return
    */
    public int[][] binDistrib(int rows, int cols, double sparsity) {
    double[][] rand = rand(rows, cols);

    for(int i = 0;i < rand.length;i++) {
    TIntArrayList sub = new TIntArrayList(
    ArrayUtils.where(rand[i], new Condition.Adapter<Double>() {
    @Override public boolean eval(double d) {
    return d >= sparsity;
    }
    }));

    int sublen = sub.size();
    int target = (int)(sparsity * cols);

    if(sublen < target) {
    int[] full = IntStream.range(0, cols).toArray();
    TIntHashSet subSet = new TIntHashSet(sub);
    TIntArrayList toFill = new TIntArrayList(
    Arrays.stream(full)
    .filter(d -> !subSet.contains(d))
    .toArray());
    int cnt = toFill.size();
    for(int x = 0;x < target - sublen;x++, cnt--) {
    int ind = nextInt(cnt);
    int item = toFill.removeAt(ind);
    rand[i][item] = sparsity;
    }
    }else if(sublen > target) {
    int cnt = sublen;
    for(int x = 0;x < sublen - target;x++, cnt--) {
    int ind = nextInt(cnt);
    int item = sub.removeAt(ind);
    rand[i][item] = 0.0;
    }
    }
    }

    int[][] retval = Arrays.stream(rand)
    .map(da -> Arrays.stream(da).mapToInt(d -> d >= sparsity ? 1 : 0).toArray())
    .toArray(int[][]::new);
    return retval;
    }

    @Override
    public double nextDouble() {
    int nd = nextInt(10000);
    double retVal = new BigDecimal(nd * .0001d, MATH_CONTEXT).doubleValue();

    System.out.println("nextDouble: " + retVal);
    return retVal;
    }

    @Override
    public int nextInt() {
    int retVal = nextInt(Integer.MAX_VALUE);

    System.out.println("nextIntNB: " + retVal);
    return retVal;
    }

    @@ -105,8 +212,16 @@ public int nextInt(int bound) {
    r = (int)((bound * (long)r) >> 31);
    else {
    r = r % bound;
    /*
    THIS CODE IS COMMENTED TO WORK IDENTICALLY WITH THE PYTHON VERSION
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    */
    }

    System.out.println("nextInt(" + bound + "): " + r);
    return r;
    }

    @@ -126,6 +241,28 @@ protected int next(int nbits) {
    return (int) x;
    }

    BigInteger bigSeed;
    /**
    * PYTHON COMPATIBLE (Protected against overflows)
    *
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int nextX(int nbits) {
    long x = seed;
    BigInteger bigX = bigSeed == null ? BigInteger.valueOf(seed) : bigSeed;
    bigX = bigX.shiftLeft(21).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftRight(35).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftLeft(4).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigSeed = bigX;
    bigX = bigX.and(BigInteger.valueOf(1L).shiftLeft(nbits).subtract(BigInteger.valueOf(1)));
    x = bigX.intValue();

    //System.out.println("x = " + x + ", seed = " + seed);
    return (int)x;
    }

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);

    89 changes: 80 additions & 9 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,9 @@

    from ctypes import c_longlong as ll


    uintType = "uint32"



    class UniversalRandom(object):
    '''
    @@ -33,7 +35,6 @@ def getSeed(self):
    return self.seed

    def rshift(self, val, n):

    if val >= 0:
    return val>>n
    else:
    @@ -77,13 +78,77 @@ def sample(self, choices, selectedIndices):
    upperBound -= 1

    selectedIndices.sort()

    print "sample: " + str(list(selectedIndices))
    return selectedIndices;



    def rand(self, rows, cols):
    """
    Returns a list of binary values of the specified shape
    @param rows (int) the number of rows
    @param cols (int) the number of columns
    """

    retval = numpy.empty((0, cols))
    for _ in range(rows):
    row = numpy.empty((cols))
    for j in range(cols):
    row[j] = self.nextDouble()

    retval = numpy.append(retval, [row], axis=0)

    return retval


    def bin_distrib(self, rows, cols, sparsity):
    """
    Returns an array of binary values of the specified shape whose
    total number of "1's" will reflect the sparsity specified.
    @param rows (int) the number of rows
    @param cols (int) the number of columns
    @param sparsity (float) number between 0 and 1, indicating percentage
    of "on" bits
    """

    if sparsity < 0 or sparsity > 1:
    raise ValueError('Sparsity must be a number between 0 - 1')

    retval = self.rand(rows, cols)

    for i in range(len(retval)):
    sub = numpy.where(retval[i] >= sparsity)[0]

    sublen = len(sub)
    target = int(sparsity * cols)

    if sublen < target:
    full = numpy.arange(0, cols, 1)
    to_fill = numpy.delete(full, sub)
    cnt = len(to_fill)
    for _ in range(target - sublen):
    ind = self.nextInt(cnt)
    item = to_fill[ind]
    to_fill = numpy.delete(to_fill, ind)
    retval[i][item] = sparsity
    cnt -= 1
    elif sublen > target:
    cnt = sublen
    for _ in range(sublen - target):
    ind = self.nextInt(cnt)
    item = sub[ind]
    sub = numpy.delete(sub, ind)
    retval[i][item] = 0.0
    cnt -= 1

    retval = (retval >= sparsity).astype(uintType)
    return retval

    def nextDouble(self):
    nd = self.nextInt(10000)
    retVal = nd * .0001

    print("nextDouble: " + str(retVal))
    return retVal

    def nextIntNB(self):
    @@ -94,7 +159,7 @@ def nextIntNB(self):
    """
    retVal = self.nextInt(self.max_java_int)

    print("nextIntNB: " + str(retVal))
    return retVal

    def nextInt(self, bound):
    @@ -110,9 +175,15 @@ def nextInt(self, bound):
    r = (bound * r) >> 31
    else:
    r = r % bound

    return r
    # u = r
    # r = u % bound
    # while u - r + m > self.max_java_int:
    # u = self._next_java_compatible(31) \
    # if self.compatibility_mode else self._next(31)
    # r = u % bound

    print("nextInt(" + str(bound) + "): " + str(r))
    return r

    def _next_java_compatible(self, nbits):
    ''' doc '''
    @@ -218,4 +289,4 @@ def _next(self, nbits):
    print "used randoms are equal ? " + str(collectedRandoms == expectedRandoms) + " --- " + str(collectedRandoms)




  3. cogmission revised this gist Sep 1, 2016. 1 changed file with 0 additions and 21 deletions.
    21 changes: 0 additions & 21 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -126,27 +126,6 @@ protected int next(int nbits) {
    return (int) x;
    }

    BigInteger bigSeed;
    /**
    * PYTHON COMPATIBLE (Protected against overflows)
    *
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int nextX(int nbits) {
    long x = seed;
    BigInteger bigX = bigSeed == null ? BigInteger.valueOf(seed) : bigSeed;
    bigX = bigX.shiftLeft(21).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftRight(35).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftLeft(4).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigSeed = bigX;
    bigX = bigX.and(BigInteger.valueOf(1L).shiftLeft(nbits).subtract(BigInteger.valueOf(1)));
    x = bigX.intValue();

    return (int)x;
    }

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);

  4. cogmission revised this gist Sep 1, 2016. 2 changed files with 17 additions and 15 deletions.
    15 changes: 8 additions & 7 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ private int[] sampleWithPrintout(TIntArrayList choices, int[] selectedIndices, L
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);
    System.out.println("randomIdx: " + randomIdx);

    collectedRandoms.add(randomIdx);
    selectedIndices[i] = (choiceSupply.removeAt(randomIdx));
    upperBound--;
    @@ -75,19 +75,23 @@ public int[] sample(TIntArrayList choices, int[] selectedIndices) {
    upperBound--;
    }
    Arrays.sort(selectedIndices);

    return selectedIndices;
    }

    @Override
    public double nextDouble() {
    int nd = nextInt(10000);
    double retVal = new BigDecimal(nd * .0001d, MATH_CONTEXT).doubleValue();

    return retVal;
    }

    @Override
    public int nextInt() {
    return nextInt(Integer.MAX_VALUE);
    int retVal = nextInt(Integer.MAX_VALUE);

    return retVal;
    }

    @Override
    @@ -100,11 +104,9 @@ public int nextInt(int bound) {
    if ((bound & m) == 0) // i.e., bound is a power of 2
    r = (int)((bound * (long)r) >> 31);
    else {
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    r = r % bound;
    }

    return r;
    }

    @@ -142,7 +144,6 @@ protected int nextX(int nbits) {
    bigX = bigX.and(BigInteger.valueOf(1L).shiftLeft(nbits).subtract(BigInteger.valueOf(1)));
    x = bigX.intValue();

    //System.out.println("x = " + x + ", seed = " + seed);
    return (int)x;
    }

    17 changes: 9 additions & 8 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ def getSeed(self):
    return self.seed

    def rshift(self, val, n):
    #return val>>n if val >= 0 else (val+0x100000000)>>n

    if val >= 0:
    return val>>n
    else:
    @@ -77,11 +77,13 @@ def sample(self, choices, selectedIndices):
    upperBound -= 1

    selectedIndices.sort()

    return selectedIndices;

    def nextDouble(self):
    nd = self.nextInt(10000)
    retVal = nd * .0001

    return retVal

    def nextIntNB(self):
    @@ -91,7 +93,9 @@ def nextIntNB(self):
    Uses maximum Java integer value.
    """
    return self.nextInt(self.max_java_int)
    retVal = self.nextInt(self.max_java_int)

    return retVal

    def nextInt(self, bound):
    ''' doc '''
    @@ -105,14 +109,11 @@ def nextInt(self, bound):
    if (bound & m) == 0:
    r = (bound * r) >> 31
    else:
    u = r
    r = u % bound
    while u - r + m < 0:
    u = self._next_java_compatible(31) \
    if self.compatibility_mode else self._next(31)

    r = r % bound

    return r


    def _next_java_compatible(self, nbits):
    ''' doc '''

  5. cogmission revised this gist Aug 11, 2016. 2 changed files with 7 additions and 6 deletions.
    2 changes: 1 addition & 1 deletion UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@ public double nextDouble() {

    @Override
    public int nextInt() {
    return next(32);
    return nextInt(Integer.MAX_VALUE);
    }

    @Override
    11 changes: 6 additions & 5 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ def __init__(self, seed, compatibility_mode=True):
    '''
    self.seed = seed
    self.uintType = "uint32"
    self.max_java_int = 2147483647
    self.compatibility_mode = compatibility_mode


    @@ -85,10 +86,12 @@ def nextDouble(self):

    def nextIntNB(self):
    """
    Next int - no bounds.
    Next int - No Bounds
    Uses maximum Java integer value.
    """
    return self._next_java_compatible(31) \
    if self.compatibility_mode else self._next(31)
    return self.nextInt(self.max_java_int)

    def nextInt(self, bound):
    ''' doc '''
    @@ -159,8 +162,6 @@ def _next(self, nbits):
    print "x = " + str(x)

    for i in xrange(0, 10):
    if i == 3:
    print "here"
    o = random.nextInt(50)
    print "x = " + str(o)

  6. cogmission revised this gist Aug 10, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -113,7 +113,7 @@ public int nextInt(int bound) {
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int nextX(int nbits) {
    protected int next(int nbits) {
    long x = seed;
    x ^= (x << 21) & 0xffffffffffffffffL;
    x ^= (x >>> 35);
    @@ -132,7 +132,7 @@ protected int nextX(int nbits) {
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    protected int nextX(int nbits) {
    long x = seed;
    BigInteger bigX = bigSeed == null ? BigInteger.valueOf(seed) : bigSeed;
    bigX = bigX.shiftLeft(21).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
  7. cogmission revised this gist Aug 10, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion universal_random.py
    Original file line number Diff line number Diff line change
    @@ -105,7 +105,8 @@ def nextInt(self, bound):
    u = r
    r = u % bound
    while u - r + m < 0:
    u = self.next(31)
    u = self._next_java_compatible(31) \
    if self.compatibility_mode else self._next(31)

    return r

  8. cogmission revised this gist Aug 10, 2016. 1 changed file with 38 additions and 14 deletions.
    52 changes: 38 additions & 14 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,42 @@
    '''
    Created on Jul 31, 2016
    Last Updated on Aug 8, 2016
    @author: cogmission
    '''

    import numpy
    import unittest

    from ctypes import c_longlong as ll



    class UniversalRandom(object):
    '''
    classdocs
    '''

    def __init__(self, seed):
    def __init__(self, seed, compatibility_mode=True):
    '''
    Constructor
    '''
    self.seed = seed
    self.uintType = "uint32"
    self.compatibility_mode = compatibility_mode



    def setSeed(self, seed):
    self.seed = seed

    def getSeed(self):
    return self.seed

    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n
    #return val>>n if val >= 0 else (val+0x100000000)>>n
    if val >= 0:
    return val>>n
    else:
    return (val+0x100000000)>>n

    def _private_sampleWithPrintouts(self, choices, selectedIndices, collectedRandoms):
    """
    @@ -79,15 +87,17 @@ def nextIntNB(self):
    """
    Next int - no bounds.
    """
    return self.next(32)
    return self._next_java_compatible(31) \
    if self.compatibility_mode else self._next(31)

    def nextInt(self, bound):
    ''' doc '''

    if bound <= 0:
    raise ValueError('bound must be positive')

    r = self.next(31)
    r = self._next_java_compatible(31) \
    if self.compatibility_mode else self._next(31)
    m = bound - 1
    if (bound & m) == 0:
    r = (bound * r) >> 31
    @@ -99,15 +109,27 @@ def nextInt(self, bound):

    return r

    def next(self, nbits):
    def _next_java_compatible(self, nbits):
    ''' doc '''

    x = self.seed & 0xffffffffffffffff #Preserve 64 bits
    x ^= (x << 21) & 0xffffffffffffffff
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    x ^= ll(x << 21).value & 0xffffffffffffffff #Preserve 64 bits
    x ^= ll(self.rshift(x, 35)).value
    x ^= ll(x << 4).value
    self.seed = x
    x &= ((1 << nbits) - 1)

    return x

    def _next(self, nbits):
    ''' doc '''

    x = self.seed
    x ^= x << 21
    x ^= self.rshift(x, 35)
    x ^= x << 4
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits
    x &= ((1 << nbits) - 1)

    return x

    @@ -136,6 +158,8 @@ def next(self, nbits):
    print "x = " + str(x)

    for i in xrange(0, 10):
    if i == 3:
    print "here"
    o = random.nextInt(50)
    print "x = " + str(o)

    @@ -187,8 +211,8 @@ def next(self, nbits):
    expectedSample = [1,2,3,7,8,9]
    expectedRandoms = [0,0,0,5,3,3]
    retVal = random._private_sampleWithPrintouts(choices, selectedIndices, collectedRandoms)
    print "samples are equal ? " + str(retVal == expectedSample)
    print "used randoms are equal ? " + str(collectedRandoms == expectedRandoms)
    print "samples are equal ? " + str(retVal == expectedSample) + " --- " + str(selectedIndices)
    print "used randoms are equal ? " + str(collectedRandoms == expectedRandoms) + " --- " + str(collectedRandoms)



  9. cogmission revised this gist Aug 10, 2016. 1 changed file with 25 additions and 2 deletions.
    27 changes: 25 additions & 2 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    package org.numenta.nupic.util;

    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.math.MathContext;
    import java.util.ArrayList;
    import java.util.Arrays;
    @@ -112,9 +113,9 @@ public int nextInt(int bound) {
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    protected int nextX(int nbits) {
    long x = seed;
    x ^= (x << 21);
    x ^= (x << 21) & 0xffffffffffffffffL;
    x ^= (x >>> 35);
    x ^= (x << 4);
    seed = x;
    @@ -123,6 +124,28 @@ protected int next(int nbits) {
    return (int) x;
    }

    BigInteger bigSeed;
    /**
    * PYTHON COMPATIBLE (Protected against overflows)
    *
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    long x = seed;
    BigInteger bigX = bigSeed == null ? BigInteger.valueOf(seed) : bigSeed;
    bigX = bigX.shiftLeft(21).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftRight(35).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigX = bigX.shiftLeft(4).xor(bigX).and(new BigInteger("ffffffffffffffff", 16));
    bigSeed = bigX;
    bigX = bigX.and(BigInteger.valueOf(1L).shiftLeft(nbits).subtract(BigInteger.valueOf(1)));
    x = bigX.intValue();

    //System.out.println("x = " + x + ", seed = " + seed);
    return (int)x;
    }

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);

  10. cogmission revised this gist Aug 8, 2016. 2 changed files with 28 additions and 30 deletions.
    31 changes: 16 additions & 15 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@
    import java.util.stream.Collectors;

    import gnu.trove.list.array.TIntArrayList;
    import gnu.trove.set.hash.TIntHashSet;

    public class UniversalRandom extends Random {
    /** serial version */
    @@ -45,32 +44,34 @@ public long getSeed() {
    }

    private int[] sampleWithPrintout(TIntArrayList choices, int[] selectedIndices, List<Integer> collectedRandoms) {
    TIntHashSet temp = new TIntHashSet();
    TIntArrayList choiceSupply = new TIntArrayList(choices);
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);
    System.out.println("randomIdx: " + randomIdx);
    collectedRandoms.add(randomIdx);
    while (temp.contains(choices.get(randomIdx))) {
    randomIdx = nextInt(upperBound);
    System.out.println("randomIdx2: " + randomIdx);
    collectedRandoms.add(randomIdx);
    }
    temp.add(selectedIndices[i] = (choices.get(randomIdx)));
    selectedIndices[i] = (choiceSupply.removeAt(randomIdx));
    upperBound--;
    }
    Arrays.sort(selectedIndices);
    return selectedIndices;
    }

    /**
    * Returns a random, sorted, and unique list of the specified sample size of
    * selections from the specified list of choices.
    *
    * @param choices
    * @param selectedIndices
    * @return an array containing a sampling of the specified choices
    */
    public int[] sample(TIntArrayList choices, int[] selectedIndices) {
    TIntHashSet temp = new TIntHashSet();
    TIntArrayList choiceSupply = new TIntArrayList(choices);
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);
    while (temp.contains(choices.get(randomIdx))) {
    randomIdx = nextInt(upperBound);
    }
    temp.add(selectedIndices[i] = (choices.get(randomIdx)));
    selectedIndices[i] = (choiceSupply.removeAt(randomIdx));
    upperBound--;
    }
    Arrays.sort(selectedIndices);
    return selectedIndices;
    @@ -192,8 +193,8 @@ public static void main(String[] args) {
    int sampleSize = 6;
    int[] selectedIndices = new int[sampleSize];
    List<Integer> collectedRandoms = new ArrayList<>();
    int[] expectedSample = {1,4,6,7,8,9};
    List<Integer> expectedRandoms = Arrays.stream(new int[] {0,0,0,5,7,3,0,6,3,6,5,7,8}).boxed().collect(Collectors.toList());
    int[] expectedSample = {1,2,3,7,8,9};
    List<Integer> expectedRandoms = Arrays.stream(new int[] {0,0,0,5,3,3}).boxed().collect(Collectors.toList());
    random.sampleWithPrintout(choices, selectedIndices, collectedRandoms);
    System.out.println("samples are equal ? " + Arrays.equals(expectedSample, selectedIndices));
    System.out.println("used randoms are equal ? " + collectedRandoms.equals(expectedRandoms));
    27 changes: 12 additions & 15 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    '''
    Created on Jul 31, 2016
    Last Updated on Aug 8, 2016
    @author: cogmission
    '''
    @@ -38,20 +39,17 @@ def _private_sampleWithPrintouts(self, choices, selectedIndices, collectedRandom
    Normal use would entail calling sample() instead of this method
    """

    temp = set()
    choiceSupply = list(choices)
    sampleSize = int(selectedIndices.size)
    upperBound = len(choices)
    for i in xrange(sampleSize):
    randomIdx = self.nextInt(upperBound)
    print "randomIdx: " + str(randomIdx)
    collectedRandoms.append(randomIdx)
    while(choices[randomIdx] in temp):
    randomIdx = self.nextInt(upperBound)
    print "randomIdx2: " + str(randomIdx)
    collectedRandoms.append(randomIdx)
    selectedIndices.itemset(i, choiceSupply[randomIdx])
    choiceSupply.remove(choiceSupply[randomIdx])
    upperBound -= 1

    temp.add(choices[randomIdx])
    selectedIndices.itemset(i, choices[randomIdx])
    selectedIndices.sort()
    return selectedIndices;

    @@ -60,16 +58,15 @@ def sample(self, choices, selectedIndices):
    Returns a random, sorted, and unique list of the specified sample size of
    selections from the specified list of choices.
    """
    temp = set()
    choiceSupply = list(choices)
    sampleSize = int(selectedIndices.size)
    upperBound = len(choices)
    for i in xrange(sampleSize):
    randomIdx = self.nextInt(upperBound)
    while(choices[randomIdx] in temp):
    randomIdx = self.nextInt(upperBound)

    temp.add(choices[randomIdx])
    selectedIndices.itemset(i, choices[randomIdx])
    selectedIndices.itemset(i, choiceSupply[randomIdx])
    choiceSupply.remove(choiceSupply[randomIdx])
    upperBound -= 1

    selectedIndices.sort()
    return selectedIndices;

    @@ -187,8 +184,8 @@ def next(self, nbits):
    sampleSize = 6
    selectedIndices = numpy.empty(sampleSize, dtype="uint32")
    collectedRandoms = []
    expectedSample = [1,4,6,7,8,9]
    expectedRandoms = [0,0,0,5,7,3,0,6,3,6,5,7,8]
    expectedSample = [1,2,3,7,8,9]
    expectedRandoms = [0,0,0,5,3,3]
    retVal = random._private_sampleWithPrintouts(choices, selectedIndices, collectedRandoms)
    print "samples are equal ? " + str(retVal == expectedSample)
    print "used randoms are equal ? " + str(collectedRandoms == expectedRandoms)
  11. cogmission revised this gist Aug 8, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion universal_random.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    '''
    Created on Jul 31, 2016
    Last Updated on Aug 8, 2016
    @author: cogmission
    '''
    @@ -78,6 +77,12 @@ def nextDouble(self):
    nd = self.nextInt(10000)
    retVal = nd * .0001
    return retVal

    def nextIntNB(self):
    """
    Next int - no bounds.
    """
    return self.next(32)

    def nextInt(self, bound):
    ''' doc '''
  12. cogmission revised this gist Aug 8, 2016. 1 changed file with 50 additions and 0 deletions.
    50 changes: 50 additions & 0 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,14 @@

    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    import java.util.stream.Collectors;

    import gnu.trove.list.array.TIntArrayList;
    import gnu.trove.set.hash.TIntHashSet;

    public class UniversalRandom extends Random {
    /** serial version */
    @@ -37,6 +44,38 @@ public long getSeed() {
    return seed;
    }

    private int[] sampleWithPrintout(TIntArrayList choices, int[] selectedIndices, List<Integer> collectedRandoms) {
    TIntHashSet temp = new TIntHashSet();
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);
    System.out.println("randomIdx: " + randomIdx);
    collectedRandoms.add(randomIdx);
    while (temp.contains(choices.get(randomIdx))) {
    randomIdx = nextInt(upperBound);
    System.out.println("randomIdx2: " + randomIdx);
    collectedRandoms.add(randomIdx);
    }
    temp.add(selectedIndices[i] = (choices.get(randomIdx)));
    }
    Arrays.sort(selectedIndices);
    return selectedIndices;
    }

    public int[] sample(TIntArrayList choices, int[] selectedIndices) {
    TIntHashSet temp = new TIntHashSet();
    int upperBound = choices.size();
    for (int i = 0; i < selectedIndices.length; i++) {
    int randomIdx = nextInt(upperBound);
    while (temp.contains(choices.get(randomIdx))) {
    randomIdx = nextInt(upperBound);
    }
    temp.add(selectedIndices[i] = (choices.get(randomIdx)));
    }
    Arrays.sort(selectedIndices);
    return selectedIndices;
    }

    @Override
    public double nextDouble() {
    int nd = nextInt(10000);
    @@ -147,6 +186,17 @@ public static void main(String[] args) {
    d = 0.5415
    d = 0.2381
    */

    random = new UniversalRandom(42);
    TIntArrayList choices = new TIntArrayList(new int[] { 1,2,3,4,5,6,7,8,9 });
    int sampleSize = 6;
    int[] selectedIndices = new int[sampleSize];
    List<Integer> collectedRandoms = new ArrayList<>();
    int[] expectedSample = {1,4,6,7,8,9};
    List<Integer> expectedRandoms = Arrays.stream(new int[] {0,0,0,5,7,3,0,6,3,6,5,7,8}).boxed().collect(Collectors.toList());
    random.sampleWithPrintout(choices, selectedIndices, collectedRandoms);
    System.out.println("samples are equal ? " + Arrays.equals(expectedSample, selectedIndices));
    System.out.println("used randoms are equal ? " + collectedRandoms.equals(expectedRandoms));
    }

    }
  13. cogmission revised this gist Aug 8, 2016. 1 changed file with 64 additions and 1 deletion.
    65 changes: 64 additions & 1 deletion universal_random.py
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,25 @@
    '''
    Created on Jul 31, 2016
    Last Updated on Aug 8, 2016
    @author: cogmission
    '''

    import numpy
    import unittest

    class UniversalRandom(object):
    '''
    classdocs
    '''


    def __init__(self, seed):
    '''
    Constructor
    '''
    self.seed = seed
    self.uintType = "uint32"


    def setSeed(self, seed):
    self.seed = seed
    @@ -25,6 +30,50 @@ def getSeed(self):
    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    def _private_sampleWithPrintouts(self, choices, selectedIndices, collectedRandoms):
    """
    Private method which is identical to the sample() method of this class.
    This method is meant for testing of identical behavior with the Java
    method of the same class.
    Normal use would entail calling sample() instead of this method
    """

    temp = set()
    sampleSize = int(selectedIndices.size)
    upperBound = len(choices)
    for i in xrange(sampleSize):
    randomIdx = self.nextInt(upperBound)
    print "randomIdx: " + str(randomIdx)
    collectedRandoms.append(randomIdx)
    while(choices[randomIdx] in temp):
    randomIdx = self.nextInt(upperBound)
    print "randomIdx2: " + str(randomIdx)
    collectedRandoms.append(randomIdx)

    temp.add(choices[randomIdx])
    selectedIndices.itemset(i, choices[randomIdx])
    selectedIndices.sort()
    return selectedIndices;

    def sample(self, choices, selectedIndices):
    """
    Returns a random, sorted, and unique list of the specified sample size of
    selections from the specified list of choices.
    """
    temp = set()
    sampleSize = int(selectedIndices.size)
    upperBound = len(choices)
    for i in xrange(sampleSize):
    randomIdx = self.nextInt(upperBound)
    while(choices[randomIdx] in temp):
    randomIdx = self.nextInt(upperBound)

    temp.add(choices[randomIdx])
    selectedIndices.itemset(i, choices[randomIdx])
    selectedIndices.sort()
    return selectedIndices;

    def nextDouble(self):
    nd = self.nextInt(10000)
    retVal = nd * .0001
    @@ -126,4 +175,18 @@ def next(self, nbits):
    d = 0.2381
    '''

    # The "expected" values are the same as produced in Java
    random = UniversalRandom(42)
    choices = [1,2,3,4,5,6,7,8,9]
    sampleSize = 6
    selectedIndices = numpy.empty(sampleSize, dtype="uint32")
    collectedRandoms = []
    expectedSample = [1,4,6,7,8,9]
    expectedRandoms = [0,0,0,5,7,3,0,6,3,6,5,7,8]
    retVal = random._private_sampleWithPrintouts(choices, selectedIndices, collectedRandoms)
    print "samples are equal ? " + str(retVal == expectedSample)
    print "used randoms are equal ? " + str(collectedRandoms == expectedRandoms)



  14. cogmission revised this gist Aug 5, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -136,7 +136,6 @@ public static void main(String[] args) {
    x = 0
    x = 21
    x = 45
    d = 0.945
    d = 0.2426
    d = 0.5214
  15. cogmission revised this gist Aug 5, 2016. 2 changed files with 51 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,14 @@
    package org.numenta.nupic.util;

    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.util.Random;

    public class UniversalRandom extends Random {
    /** serial version */
    private static final long serialVersionUID = 1L;

    private static final MathContext MATH_CONTEXT = new MathContext(9);

    long seed;

    @@ -19,6 +23,7 @@ public UniversalRandom(long seed) {
    *
    * @param seed the value with which to be initialized
    */
    @Override
    public void setSeed(long seed) {
    this.seed = seed;
    }
    @@ -32,10 +37,19 @@ public long getSeed() {
    return seed;
    }

    @Override
    public double nextDouble() {
    int nd = nextInt(10000);
    double retVal = new BigDecimal(nd * .0001d, MATH_CONTEXT).doubleValue();
    return retVal;
    }

    @Override
    public int nextInt() {
    return next(32);
    }

    @Override
    public int nextInt(int bound) {
    if (bound <= 0)
    throw new IllegalArgumentException(BadBound);
    @@ -96,6 +110,12 @@ public static void main(String[] args) {
    System.out.println("x = " + o);
    }

    random = new UniversalRandom(42);
    for(int i = 0;i < 10;i++) {
    double o = random.nextDouble();
    System.out.println("d = " + o);
    }

    ///////////////////////////////////
    // Values Seen in Python //
    ///////////////////////////////////
    @@ -116,6 +136,17 @@ public static void main(String[] args) {
    x = 0
    x = 21
    x = 45
    d = 0.945
    d = 0.2426
    d = 0.5214
    d = 0.0815
    d = 0.0988
    d = 0.5497
    d = 0.4013
    d = 0.4559
    d = 0.5415
    d = 0.2381
    */
    }

    20 changes: 20 additions & 0 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,11 @@ def getSeed(self):
    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    def nextDouble(self):
    nd = self.nextInt(10000)
    retVal = nd * .0001
    return retVal

    def nextInt(self, bound):
    ''' doc '''

    @@ -87,6 +92,10 @@ def next(self, nbits):
    ######################################
    ## Values Seen in Java ##
    ######################################
    random = UniversalRandom(42)
    for i in range(10):
    o = random.nextDouble()
    print "d = " + str(o)

    '''
    e = 83200
    @@ -105,5 +114,16 @@ def next(self, nbits):
    x = 0
    x = 21
    x = 45
    d = 0.945
    d = 0.2426
    d = 0.5214
    d = 0.0815
    d = 0.0988
    d = 0.5497
    d = 0.4013
    d = 0.4559
    d = 0.5415
    d = 0.2381
    '''

  16. cogmission revised this gist Aug 4, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,24 @@ public UniversalRandom(long seed) {
    this.seed = seed;
    }

    /**
    * Sets the long value used as the initial seed
    *
    * @param seed the value with which to be initialized
    */
    public void setSeed(long seed) {
    this.seed = seed;
    }

    /**
    * Returns the long value used as the initial seed
    *
    * @return the initial seed value
    */
    public long getSeed() {
    return seed;
    }

    public int nextInt() {
    return next(32);
    }
  17. cogmission revised this gist Aug 4, 2016. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion universal_random.py
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,12 @@ def __init__(self, seed):
    '''
    self.seed = seed

    def setSeed(self, seed):
    self.seed = seed

    def getSeed(self):
    return self.seed

    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    @@ -45,7 +51,7 @@ def next(self, nbits):
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits

    return x

  18. cogmission revised this gist Aug 1, 2016. 1 changed file with 104 additions and 0 deletions.
    104 changes: 104 additions & 0 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    package org.numenta.nupic.util;

    import java.util.Random;

    public class UniversalRandom extends Random {
    /** serial version */
    private static final long serialVersionUID = 1L;

    long seed;

    static final String BadBound = "bound must be positive";

    public UniversalRandom(long seed) {
    this.seed = seed;
    }

    public int nextInt() {
    return next(32);
    }

    public int nextInt(int bound) {
    if (bound <= 0)
    throw new IllegalArgumentException(BadBound);

    int r = next(31);
    int m = bound - 1;
    if ((bound & m) == 0) // i.e., bound is a power of 2
    r = (int)((bound * (long)r) >> 31);
    else {
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    }
    return r;
    }

    /**
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    long x = seed;
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    seed = x;
    x &= ((1L << nbits) - 1);

    return (int) x;
    }

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);

    long s = 2858730232218250L;
    long e = (s >>> 35);
    System.out.println("e = " + e);

    int x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    for(int i = 0;i < 10;i++) {
    int o = random.nextInt(50);
    System.out.println("x = " + o);
    }

    ///////////////////////////////////
    // Values Seen in Python //
    ///////////////////////////////////
    /*
    * e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    */
    }

    }
  19. cogmission revised this gist Aug 1, 2016. 2 changed files with 2 additions and 105 deletions.
    103 changes: 0 additions & 103 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,103 +0,0 @@
    '''
    Created on Jul 31, 2016

    @author: cogmission
    '''

    class UniversalRandom(object):
    '''
    classdocs
    '''


    def __init__(self, seed):
    '''
    Constructor
    '''
    self.seed = seed

    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    def nextInt(self, bound):
    ''' doc '''

    if bound <= 0:
    raise ValueError('bound must be positive')

    r = self.next(31)
    m = bound - 1
    if (bound & m) == 0:
    r = (bound * r) >> 31
    else:
    u = r
    r = u % bound
    while u - r + m < 0:
    u = self.next(31)

    return r

    def next(self, nbits):
    ''' doc '''

    x = self.seed & 0xffffffffffffffff #Preserve 64 bits
    x ^= (x << 21) & 0xffffffffffffffff
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits

    return x


    if __name__ == '__main__':

    random = UniversalRandom(42)

    s = 2858730232218250
    e = random.rshift(s, 35)
    print "e = " + str(e)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    for i in xrange(0, 10):
    o = random.nextInt(50)
    print "x = " + str(o)


    ######################################
    ## Values Seen in Java ##
    ######################################

    '''
    e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    '''

    4 changes: 2 additions & 2 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -40,12 +40,12 @@ def nextInt(self, bound):
    def next(self, nbits):
    ''' doc '''

    x = self.seed & 0xffffffffffffffff
    x = self.seed & 0xffffffffffffffff #Preserve 64 bits
    x ^= (x << 21) & 0xffffffffffffffff
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffffffffffff
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits

    return x

  20. cogmission revised this gist Aug 1, 2016. 1 changed file with 90 additions and 91 deletions.
    181 changes: 90 additions & 91 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,104 +1,103 @@
    package org.numenta.nupic.util;
    '''
    Created on Jul 31, 2016

    import java.util.Random;
    @author: cogmission
    '''

    public class UniversalRandom extends Random {
    /** serial version */
    private static final long serialVersionUID = 1L;
    class UniversalRandom(object):
    '''
    classdocs
    '''

    long seed;

    static final String BadBound = "bound must be positive";

    public UniversalRandom(long seed) {
    this.seed = seed;
    }

    public int nextInt() {
    return next(32);
    }

    public int nextInt(int bound) {
    if (bound <= 0)
    throw new IllegalArgumentException(BadBound);

    int r = next(31);
    int m = bound - 1;
    if ((bound & m) == 0) // i.e., bound is a power of 2
    r = (int)((bound * (long)r) >> 31);
    else {
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    }
    return r;
    }

    /**
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    long x = seed;
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    seed = x;
    x &= ((1L << nbits) - 1);
    def __init__(self, seed):
    '''
    Constructor
    '''
    self.seed = seed

    return (int) x;
    }
    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);
    def nextInt(self, bound):
    ''' doc '''

    long s = 2858730232218250L;
    long e = (s >>> 35);
    System.out.println("e = " + e);
    if bound <= 0:
    raise ValueError('bound must be positive')

    int x = random.nextInt(50);
    System.out.println("x = " + x);
    r = self.next(31)
    m = bound - 1
    if (bound & m) == 0:
    r = (bound * r) >> 31
    else:
    u = r
    r = u % bound
    while u - r + m < 0:
    u = self.next(31)

    x = random.nextInt(50);
    System.out.println("x = " + x);
    return r

    x = random.nextInt(50);
    System.out.println("x = " + x);
    def next(self, nbits):
    ''' doc '''

    x = random.nextInt(50);
    System.out.println("x = " + x);
    x = self.seed & 0xffffffffffffffff #Preserve 64 bits
    x ^= (x << 21) & 0xffffffffffffffff
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffff #Preserve the integer bits

    x = random.nextInt(50);
    System.out.println("x = " + x);

    for(int i = 0;i < 10;i++) {
    int o = random.nextInt(50);
    System.out.println("x = " + o);
    }

    ///////////////////////////////////
    // Values Seen in Python //
    ///////////////////////////////////
    /*
    * e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    */
    }
    return x


    if __name__ == '__main__':

    }
    random = UniversalRandom(42)

    s = 2858730232218250
    e = random.rshift(s, 35)
    print "e = " + str(e)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    x = random.nextInt(50)
    print "x = " + str(x)
    for i in xrange(0, 10):
    o = random.nextInt(50)
    print "x = " + str(o)


    ######################################
    ## Values Seen in Java ##
    ######################################

    '''
    e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    '''

  21. cogmission revised this gist Aug 1, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    package org.numenta.nupic.util;

    public class UniversalRandom {
    import java.util.Random;

    public class UniversalRandom extends Random {
    /** serial version */
    private static final long serialVersionUID = 1L;

    long seed;

    static final String BadBound = "bound must be positive";
  22. cogmission created this gist Aug 1, 2016.
    99 changes: 99 additions & 0 deletions UniversalRandom.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    package org.numenta.nupic.util;

    public class UniversalRandom {
    long seed;

    static final String BadBound = "bound must be positive";

    public UniversalRandom(long seed) {
    this.seed = seed;
    }

    public int nextInt() {
    return next(32);
    }

    public int nextInt(int bound) {
    if (bound <= 0)
    throw new IllegalArgumentException(BadBound);

    int r = next(31);
    int m = bound - 1;
    if ((bound & m) == 0) // i.e., bound is a power of 2
    r = (int)((bound * (long)r) >> 31);
    else {
    for (int u = r;
    u - (r = u % bound) + m < 0;
    u = next(31))
    ;
    }
    return r;
    }

    /**
    * Implementation of George Marsaglia's elegant Xorshift random generator
    * 30% faster and better quality than the built-in java.util.random see also
    * see http://www.javamex.com/tutorials/random_numbers/xorshift.shtml
    */
    protected int next(int nbits) {
    long x = seed;
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    seed = x;
    x &= ((1L << nbits) - 1);

    return (int) x;
    }

    public static void main(String[] args) {
    UniversalRandom random = new UniversalRandom(42);

    long s = 2858730232218250L;
    long e = (s >>> 35);
    System.out.println("e = " + e);

    int x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    x = random.nextInt(50);
    System.out.println("x = " + x);

    for(int i = 0;i < 10;i++) {
    int o = random.nextInt(50);
    System.out.println("x = " + o);
    }

    ///////////////////////////////////
    // Values Seen in Python //
    ///////////////////////////////////
    /*
    * e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    */
    }

    }
    103 changes: 103 additions & 0 deletions universal_random.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    '''
    Created on Jul 31, 2016
    @author: cogmission
    '''

    class UniversalRandom(object):
    '''
    classdocs
    '''


    def __init__(self, seed):
    '''
    Constructor
    '''
    self.seed = seed

    def rshift(self, val, n):
    return val>>n if val >= 0 else (val+0x100000000)>>n

    def nextInt(self, bound):
    ''' doc '''

    if bound <= 0:
    raise ValueError('bound must be positive')

    r = self.next(31)
    m = bound - 1
    if (bound & m) == 0:
    r = (bound * r) >> 31
    else:
    u = r
    r = u % bound
    while u - r + m < 0:
    u = self.next(31)

    return r

    def next(self, nbits):
    ''' doc '''

    x = self.seed & 0xffffffffffffffff
    x ^= (x << 21) & 0xffffffffffffffff
    x ^= self.rshift(x, 35) & 0xffffffffffffffff
    x ^= (x << 4) & 0xffffffffffffffff
    self.seed = x
    x &= ((1 << nbits) - 1) & 0xffffffffffffffff

    return x


    if __name__ == '__main__':

    random = UniversalRandom(42)

    s = 2858730232218250
    e = random.rshift(s, 35)
    print "e = " + str(e)

    x = random.nextInt(50)
    print "x = " + str(x)

    x = random.nextInt(50)
    print "x = " + str(x)

    x = random.nextInt(50)
    print "x = " + str(x)

    x = random.nextInt(50)
    print "x = " + str(x)

    x = random.nextInt(50)
    print "x = " + str(x)

    for i in xrange(0, 10):
    o = random.nextInt(50)
    print "x = " + str(o)


    ######################################
    ## Values Seen in Java ##
    ######################################

    '''
    e = 83200
    x = 0
    x = 26
    x = 14
    x = 15
    x = 38
    x = 47
    x = 13
    x = 9
    x = 15
    x = 31
    x = 6
    x = 3
    x = 0
    x = 21
    x = 45
    '''