Hi everybody

socram8888

Member
Contributor
Code:
/*
* Copyright 2013 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package org.terasology.utilities.procedural;
 
/**
* Interface for random number generators.
* @author socram8888
*/
public abstract class Random {
    private static final char[] VALID_CHARS = new char[] {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
 
    /**
    * @return Random integer from Integer.MIN_VALUE to Integer.MAX_VALUE
    */
    public abstract int nextInt();
 
    /**
    * @param max
    * @return Random integer from 0 (inclusive) to max (exclusive)
    */
    public int nextInt(int max) {
        return (int) (max * nextDouble());
    }
 
    /**
    * @param min
    * @param max
    * @return Random integer from min (inclusive) to max (inclusive)
    */
    public int nextInt(int min, int max) {
        return min + nextInt(max - min + 1);
    }
 
    /**
    * @return Random long from Long.MIN_VALUE to Long.MAX_VALUE
    */
    public long nextLong() {
        return (long) nextInt() << 32 | (long) nextInt() & 0xFFFFFFFFL;
    }
 
    /**
    * @param max
    * @return Random long from 0 (inclusive) to max (exclusive)
    */
    public long nextLong(long max) {
        return (long) (max * nextDouble());
    }
 
    /**
    * @param min
    * @param max
    * @return Random long from min (inclusive) to max (inclusive)
    */
    public long nextLong(long min, long max) {
        return min + nextLong(max - min + 1);
    }
 
    /**
    * @return Random float from 0 (inclusive) to 1 (exclusive)
    */
    public float nextFloat() {
        return (float) (nextInt() & 0x7FFFFFFF) / ((float) Integer.MAX_VALUE + 1.0f);
    }
 
    /**
    * @return Random double from 0 (inclusive) to 1 (exclusive)
    */
    public double nextDouble() {
        return (double) (nextLong() & 0x7FFFFFFFFFFFFFFFL) / ((double) Long.MAX_VALUE + 1.0);
    }
 
    /**
    * @return Random boolean
    */
    public boolean nextBoolean() {
        return nextInt() > 0;
    }
 
    /**
    * @param len String length
    * @return Random alphanumeric string
    */
    public String nextString(int len) {
        char[] chars = new char[len];
        for (int i = 0; i < len; i++) {
            chars[i] = VALID_CHARS[nextInt(VALID_CHARS.length)];
        }
        return new String(chars);
    }
 
    /**
    * Returns a random item from the given list
    *
    * @param list
    * @return An element, or null is the list is empty
    */
    public <T> T nextItem(List<T> list) {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(nextInt(list.size()));
    }
}
How about this one? It's an abstract class with some stuff for simplicity. Creating a new RNG it's a matter of implementing a simple nextInt call. It's missing some methods for Vectors, though.
 

Immortius

Lead Software Architect
Contributor
Architecture
GUI
The API is looking good. And having it abstract is a smart approach. Could also it concrete and taking a RandomSource object that implements nextInt(), but I'm not that fussed. (How are those unit tests coming along by the way? :p)

One small issue/concern: with

Code:
return (long) nextInt() << 32 | (long) nextInt() & 0xFFFFFFFFL;
Is the & 0xFFFFFFFFL needed?
 

socram8888

Member
Contributor
Actually it is not. Can be modified to:

Code:
return (long) nextInt() << 32 ^ (long) nextInt();
When you expand an int to a long, if the int was negative, the upper bits from the long are set to 1, in order to keep the sign:
Code:
(long) 0x01234567 -> 0x0000000001234567L
(long) 0x89ABCDEF -> 0xFFFFFFFF89ABCDEFL
Using the AND I was removing those 32 upper bits, wether they were one or not. But it is also possible to employ a XOR, so if the lower int was negative, upper bits from the long are inverted, which has no side effect to randomness and is faster.
 
Top