/** * @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 boolean */ public boolean nextBoolean() { return nextInt() < 0; }
/** * @return Random float from 0 (inclusive) to 1 (exclusive) */ public float nextFloat() { return (nextInt() & 0x7FFFFFFF) / (Integer.MAX_VALUE + 1.0f); }
/** * @param min * @param max * @return Random float from min (inclusive) to max (inclusive) */ public float nextFloat(float min, float max) { return min + (max - min) * (nextInt() & 0x7FFFFFFF) / Integer.MAX_VALUE; }
/** * Returns a random alphanumeric string with a certain length * * @param len String length * @return */ public String nextString(int len) { char[] chars = new char[len]; for (int i = 0; i < len; i++) { chars[i] = ALPHANUMERIC_CHARS[nextInt(ALPHANUMERIC_CHARS.length)]; } return new String(chars); }
/** * Returns a random item from the given list, or null is the list is empty * * @param list * @return */ public <T> T nextItem(List<T> list) { if (list.isEmpty()) { return null; } return list.get(nextInt(list.size())); }
private StaticSound getRandomExplosionSound() { return explosionSounds.get(random.nextInt(0, explosionSounds.size() - 1)).get(); }
/** * 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())); } }
public DropParser invoke() { resultDrop = drop; int timesIndex = resultDrop.indexOf('*'); int countMin = 1; int countMax = 1; if (timesIndex > -1) { String timesStr = resultDrop.substring(0, timesIndex); int minusIndex = timesStr.indexOf('-'); if (minusIndex > -1) { countMin = Integer.parseInt(timesStr.substring(0, minusIndex)); countMax = Integer.parseInt(timesStr.substring(minusIndex + 1)); } else { countMin = Integer.parseInt(timesStr); countMax = countMin; } resultDrop = resultDrop.substring(timesIndex + 1); } count = rnd.nextInt(countMin, countMax); return this; } }
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); } }
@Override public void onEmission(TextureOffsetGeneratorComponent component, ParticleData particleData, Random random) { if (component.validOffsets.size() == 0) { return; } final int randomOffsetIndex = random.nextInt(component.validOffsets.size()); final Vector2f randomOffset = component.validOffsets.get(randomOffsetIndex); particleData.textureOffset.set(randomOffset.getX(), randomOffset.getY()); } }
/** * Computes n random offset values for each block part texture. * * @param blockAppearance the block appearance information to generate offsets from * @param scale the scale of the texture area (should be in 0 < scale <= 1.0) * * @return a list of random offsets sampled from all block parts */ private List<Vector2f> computeOffsets(BlockAppearance blockAppearance, float scale) { final float relativeTileSize = worldAtlas.getRelativeTileSize(); final int absoluteTileSize = worldAtlas.getTileSize(); final float pixelSize = relativeTileSize / absoluteTileSize; final int spriteWidth = TeraMath.ceilToInt(scale * absoluteTileSize); final Stream<Vector2f> baseOffsets = Arrays.stream(BlockPart.sideValues()).map(blockAppearance::getTextureAtlasPos); return baseOffsets.flatMap(baseOffset -> IntStream.range(0, 8).boxed().map(i -> new Vector2f(baseOffset).add(random.nextInt(absoluteTileSize - spriteWidth) * pixelSize, random.nextInt(absoluteTileSize - spriteWidth) * pixelSize) ) ).collect(Collectors.toList()); }
@Test public void testColorTextures() { Random r = new FastRandom(123456); for (int i = 0; i < 10; i++) { int rgba = r.nextInt(); Color red = new Color(rgba); ResourceUrn textureUriForColor = TextureUtil.getTextureUriForColor(red); String simpleString = textureUriForColor.toString(); Optional<Texture> tex = Assets.getTexture(simpleString); assertTrue(tex.isPresent()); ByteBuffer dataBuffer = tex.get().getData().getBuffers()[0]; int firstPixel = dataBuffer.asIntBuffer().get(0); Assert.assertEquals(rgba, firstPixel); } }
@Test public void testSetEventCall() { setting.subscribe(listener); Random random = new FastRandom(); final int maxSetValueCount = 50; int expectedEventCallCount = 0; for (int i = 0; i < maxSetValueCount; i++) { int randomInt = random.nextInt(-50, 150); expectedEventCallCount += setting.setValue(randomInt) ? 1 : 0; } assertEquals(expectedEventCallCount, eventCallCount); }