Search in sources :

Example 6 with FastRandom

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);
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) FastRandom(org.terasology.utilities.random.FastRandom) ByteBuffer(java.nio.ByteBuffer)

Example 7 with FastRandom

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);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) FastRandom(org.terasology.utilities.random.FastRandom) MeshComponent(org.terasology.rendering.logic.MeshComponent) BlockComponent(org.terasology.world.block.BlockComponent) Component(org.terasology.entitySystem.Component) LocationComponent(org.terasology.logic.location.LocationComponent) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 8 with FastRandom

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);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) MeshComponent(org.terasology.rendering.logic.MeshComponent) PojoEntityManager(org.terasology.entitySystem.entity.internal.PojoEntityManager) FastRandom(org.terasology.utilities.random.FastRandom) MeshComponent(org.terasology.rendering.logic.MeshComponent) BlockComponent(org.terasology.world.block.BlockComponent) Component(org.terasology.entitySystem.Component) LocationComponent(org.terasology.logic.location.LocationComponent) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 9 with FastRandom

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()));
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) Color(org.terasology.rendering.nui.Color) FastRandom(org.terasology.utilities.random.FastRandom)

Example 10 with FastRandom

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);
}
Also used : MersenneRandom(org.terasology.utilities.random.MersenneRandom) FastRandom(org.terasology.utilities.random.FastRandom) MersenneRandom(org.terasology.utilities.random.MersenneRandom) FastRandom(org.terasology.utilities.random.FastRandom) Test(org.junit.Test)

Aggregations

FastRandom (org.terasology.utilities.random.FastRandom)14 LocationComponent (org.terasology.logic.location.LocationComponent)4 Random (org.terasology.utilities.random.Random)4 Test (org.junit.Test)3 Component (org.terasology.entitySystem.Component)3 MeshComponent (org.terasology.rendering.logic.MeshComponent)3 ByteBuffer (java.nio.ByteBuffer)2 PojoEntityManager (org.terasology.entitySystem.entity.internal.PojoEntityManager)2 Color (org.terasology.rendering.nui.Color)2 BlockComponent (org.terasology.world.block.BlockComponent)2 WorldInfo (org.terasology.world.internal.WorldInfo)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 TreeFacet (org.terasology.core.world.generator.facets.TreeFacet)1 TreeGenerator (org.terasology.core.world.generator.trees.TreeGenerator)1 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)1