use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.
the class TextureDataFactory method createWhiteNoiseTexture.
public static TextureData createWhiteNoiseTexture(int size, long seed, int min, int max) {
int width = size;
int height = size;
ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height);
Random rng = new FastRandom(seed);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255));
data.put((byte) 255);
}
}
// The buffer must be reset back to the initial position before passing it onward.
data.rewind();
return new TextureData(width, height, new ByteBuffer[] { data }, WrapMode.REPEAT, FilterMode.NEAREST);
}
use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.
the class EntityCreateBenchmark method setup.
@Override
public void setup() {
FastRandom rand = new FastRandom(0L);
rawEntityData = Lists.newArrayList();
for (int i = 0; i < 1000; ++i) {
List<Component> entityData = Lists.newArrayList();
if (rand.nextFloat() < 0.75f) {
entityData.add(new LocationComponent());
}
if (rand.nextFloat() < 0.5f) {
entityData.add(new MeshComponent());
}
if (rand.nextFloat() < 0.25f) {
entityData.add(new BlockComponent());
}
rawEntityData.add(entityData);
}
}
use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.
the class IterateSingleComponentBenchmark method setup.
@Override
public void setup() {
FastRandom rand = new FastRandom(0L);
rawEntityData = Lists.newArrayList();
for (int i = 0; i < 1000; ++i) {
List<Component> entityData = Lists.newArrayList();
if (rand.nextFloat() < 0.75f) {
entityData.add(new LocationComponent());
}
if (rand.nextFloat() < 0.5f) {
entityData.add(new MeshComponent());
}
if (rand.nextFloat() < 0.25f) {
entityData.add(new BlockComponent());
}
rawEntityData.add(entityData);
}
entityManager = new PojoEntityManager();
for (List<Component> rawEntity : rawEntityData) {
entityManager.create(rawEntity);
}
}
use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.
the class PlayerConfig method defaultPlayerColor.
/**
* Randomly generates a default color for the player via a random int generator using FastRandom object.
*
* @return a Color object with the player's default color.
*/
private Color defaultPlayerColor() {
Random rng = new FastRandom();
List<Color> colors = CieCamColors.L65C65;
return colors.get(rng.nextInt(colors.size()));
}
use of org.terasology.utilities.random.FastRandom in project Terasology by MovingBlocks.
the class MersenneRandomTest method testSpeed.
/**
* Perform some speed tests and write results to logger
*/
@Test
public void testSpeed() {
final long seed = 4357;
final int warmUpCount = 10000;
final int count = 10000000;
int sum;
long start;
logger.info("Time to test grabbing {} ints", count);
// -------------------------------------------------------
java.util.Random rr = new java.util.Random(seed);
// warmup
sum = 0;
for (int j = 0; j < warmUpCount; j++) {
sum += rr.nextInt();
}
sum = 0;
start = System.nanoTime();
for (int j = 0; j < count; j++) {
sum += rr.nextInt();
}
logger.info("java.util.Random: {}ms.", (System.nanoTime() - start) / 1000000);
logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
// -------------------------------------------------------
FastRandom fr = new FastRandom(seed);
// warmup
sum = 0;
for (int j = 0; j < warmUpCount; j++) {
sum += fr.nextInt();
}
sum = 0;
start = System.nanoTime();
for (int j = 0; j < count; j++) {
sum += fr.nextInt();
}
logger.info("FastRandom: {}ms.", (System.nanoTime() - start) / 1000000);
logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
// -------------------------------------------------------
MersenneRandom r = new MersenneRandom(seed);
// warmup
sum = 0;
for (int j = 0; j < warmUpCount; j++) {
sum += r.nextInt();
}
sum = 0;
start = System.nanoTime();
for (int j = 0; j < count; j++) {
sum += r.nextInt();
}
logger.info("MersenneRandom: {}ms.", (System.nanoTime() - start) / 1000000);
logger.trace("Use the result so that JVM doesn't skip the computation - here it is: {}", sum);
}
Aggregations