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()));
}
}