下面列出了java.util.Random#nextFloat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void build(double[] x, int a, int g, int z, int n, int p, Random random) {
int fromIndex = 0;
double negativeValue = -random.nextFloat();
double positiveValue = random.nextFloat();
writeValue(x, negativeValue, fromIndex, n);
fromIndex += n;
writeValue(x, -0.0d, fromIndex, g);
fromIndex += g;
writeValue(x, 0.0d, fromIndex, z);
fromIndex += z;
writeValue(x, positiveValue, fromIndex, p);
fromIndex += p;
writeValue(x, Double.NaN, fromIndex, a);
}
@SideOnly(Side.CLIENT)
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
Blocks.LEAVES.randomDisplayTick(stateIn, worldIn, pos, rand);
if (rand.nextInt(40) == 0) {
int j = rand.nextInt(2) * 2 - 1;
int k = rand.nextInt(2) * 2 - 1;
double d0 = pos.getX() + 0.5D + 0.25D * j;
double d1 = pos.getY() - 0.15D;
double d2 = pos.getZ() + 0.5D + 0.25D * k;
double d3 = rand.nextFloat() * j * 0.1D;
double d4 = ((rand.nextFloat()) * 0.055D) + 0.015D;
double d5 = rand.nextFloat() * k * 0.1D;
SakuraMain.proxy.spawnParticle(SakuraParticleType.MAPLERED, d0, d1, d2, d3, -d4, d5);
}
}
/**
* @return A randomly generated ArrayList of fractions (stored as doubles)
*/
public ArrayList<Double> randomFractionValues() {
Random generator = new Random();
Random fractionRand = new Random();
currentRowNumber = (int)(generator.nextFloat() * rowCount);
System.out.println("Current row: " + (currentRowNumber + 1));
currentRow = currentSheet.getRow(currentRowNumber);
ArrayList<Double> cardValues = new ArrayList<Double>();
for (int x = 0; x < 6; x++) {
cardValues.add(((int)(fractionRand.nextDouble() * 10)) / 10.0);
}
int RandomInsert1 = (int)(generator.nextFloat() * 6);
int RandomInsert2;
do {
RandomInsert2 = (int)(generator.nextFloat() * 6);
} while (RandomInsert2 == RandomInsert1); // The two values must be in distinct NumberCards (i.e. not the same card!)
cardValues.set(RandomInsert1, convertFractiontoDecimal(currentRow.getCell(1).getStringCellValue()));
cardValues.set(RandomInsert2, convertFractiontoDecimal(currentRow.getCell(3).getStringCellValue()));
return cardValues;
}
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(IBlockState state, World world, BlockPos pos, Random rand) {
super.randomDisplayTick(state, world, pos, rand);
if (world.getBlockState(pos.up()).getMaterial() != Material.WATER && rand.nextInt(3) == 0)
{
if (this.getHeatStrength(world, pos) > 0)
{
float steamX = pos.getX() + 0.5F;
float steamY = pos.getY() + 0.9F;
float steamZ = pos.getZ() + 0.5F;
float steamRandX = rand.nextFloat() * 0.6F - 0.3F;
float steamRandZ = rand.nextFloat() * 0.6F - 0.3F;
double gRand1 = rand.nextGaussian() * 0.01D;
double gRand2 = rand.nextGaussian() * 0.01D;
double gRand3 = rand.nextGaussian() * 0.01D;
world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, (steamX + steamRandX), steamY, (steamZ + steamRandZ), gRand1, gRand2, gRand3);
}
}
}
/**
* Helper method that generates a random value for a given data type.
*
* @param dataType Data type for which to generate the random value
* @return Random value for the data type
*/
public static Object getRandomValue(Random random, FieldSpec.DataType dataType) {
switch (dataType) {
case INT:
return random.nextInt();
case LONG:
return random.nextLong();
case FLOAT:
return random.nextFloat();
case DOUBLE:
return random.nextDouble();
case STRING:
return StringUtil
.sanitizeStringValue(RandomStringUtils.random(random.nextInt(MAX_STRING_LENGTH)), Integer.MAX_VALUE);
default:
throw new UnsupportedOperationException("Unsupported data type for random value generator: " + dataType);
}
}
@Test
public void testRandomInput() throws Exception {
Random rand = new Random(1337);
final int numElements = 256;
byte[] byteData = new byte[numElements * 4];
float[] values = new float[numElements];
for (int i = 0; i < numElements; ++i) {
float f = rand.nextFloat() * 1024.0f;
values[i] = f;
int fAsInt = Float.floatToIntBits(f);
byteData[i] = (byte) (fAsInt & 0xFF);
byteData[numElements + i] = (byte) ((fAsInt >> 8) & 0xFF);
byteData[numElements * 2 + i] = (byte) ((fAsInt >> 16) & 0xFF);
byteData[numElements * 3 + i] = (byte) ((fAsInt >> 24) & 0xFF);
}
testReader(byteData, values);
}
void build(double[] x, int a, int g, int z, int n, int p, Random random) {
int fromIndex = 0;
double negativeValue = -random.nextFloat();
double positiveValue = random.nextFloat();
writeValue(x, negativeValue, fromIndex, n);
fromIndex += n;
writeValue(x, -0.0d, fromIndex, g);
fromIndex += g;
writeValue(x, 0.0d, fromIndex, z);
fromIndex += z;
writeValue(x, positiveValue, fromIndex, p);
fromIndex += p;
writeValue(x, Double.NaN, fromIndex, a);
}
private void play() {
Random random = new Random();
if (view.isReady()) {
float maxScale = view.getMaxScale();
float minScale = view.getMinScale();
float scale = (random.nextFloat() * (maxScale - minScale)) + minScale;
PointF center = new PointF(random.nextInt(view.getSWidth()), random.nextInt(view.getSHeight()));
view.setPin(center);
AnimationBuilder animationBuilder = view.animateScaleAndCenter(scale, center);
if (getPage() == 3) {
animationBuilder.withDuration(2000).withEasing(EASE_OUT_QUAD).withInterruptible(false).start();
} else {
animationBuilder.withDuration(750).start();
}
}
}
/**
* Creates a new goo transition.
*
* @param columns the number of columns to use.
*/
public GooTransition2D(int columns) {
Random r = new Random();
offset = new float[columns];
accel = new float[columns];
boolean ok = false;
long seed = System.currentTimeMillis();
while (!ok) {
seed++;
r.setSeed(seed);
ok = true;
for (int a = 0; a < columns && ok; a++) {
offset[a] = -r.nextFloat();
accel[a] = 4 * r.nextFloat() + 0.2f;
if (accel[a] + 1 + offset[a] < 1.2) {
ok = false;
}
}
if (ok) {
//make sure at least one strand takes up the whole time t=[0,1]
boolean atLeastOneSlowOne = false;
for (int a = 0; a < columns && !atLeastOneSlowOne; a++) {
atLeastOneSlowOne = accel[a] + 1 + offset[a] < 1.3;
}
ok = atLeastOneSlowOne;
}
}
}
public GLBubble(float[] color, float startX, float fromY, float toY, float size, Random random) {
super(color);
this.random = random;
update(startX, fromY, toY, size);
float[] vertices = new float[(POINTS_PER_CIRCLE + 1) * COORDS_PER_VERTEX];
short[] indices = new short[POINTS_PER_CIRCLE * COORDS_PER_VERTEX];
int i;
for (i = 0; i < indices.length / COORDS_PER_VERTEX - 1; i++) {
indices[COORDS_PER_VERTEX * i] = 0;
indices[COORDS_PER_VERTEX * i + 1] = (short) (i + 1);
indices[COORDS_PER_VERTEX * i + 2] = (short) (i + 2);
}
// connect first and last elements
indices[COORDS_PER_VERTEX * i] = 0;
indices[COORDS_PER_VERTEX * i + 1] = (short) (i + 1);
indices[COORDS_PER_VERTEX * i + 2] = (short) 1;
ByteBuffer verticesByteBuffer = ByteBuffer.allocateDirect(vertices.length * SIZE_OF_FLOAT);
verticesByteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = verticesByteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer indicesByteBuffer = ByteBuffer.allocateDirect(indices.length * SIZE_OF_SHORT);
indicesByteBuffer.order(ByteOrder.nativeOrder());
shortBuffer = indicesByteBuffer.asShortBuffer();
shortBuffer.put(indices);
shortBuffer.position(0);
angle = (float) (random.nextFloat() * 2 * Math.PI);
}
private float getData(ParticleEffect p) {
Random random = new Random();
float data = p.getData();
if (p.getData() == -1) {
data = random.nextFloat();
}
return data;
}
private void generateWaveArray(){
int count = mWidth / (WAVE_WIDTH + WAVE_OFFSET);
mWaveArray = new float[count];
Random random = new Random();
random.setSeed(mMusicDurationMs);
for(int i = 0; i < count; i++){
mWaveArray[i] = random.nextFloat();
if(mWaveArray[i] < MIN_WAVE_RATE){
mWaveArray[i] += MIN_WAVE_RATE;
}
}
}
@Override
public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player) {
if (player.worldObj.isRemote) {
return false;
}
Block block = player.worldObj.getBlock(x, y, z);
if (block instanceof IShearable) {
IShearable target = (IShearable) block;
if (target.isShearable(stack, player.worldObj, x, y, z)) {
ArrayList<ItemStack> drops = target.onSheared(stack, player.worldObj, x, y, z,
EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, stack));
Random rand = new Random();
for (ItemStack drop : drops) {
float f = 0.7F;
double d = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(player.worldObj, (double) x + d, (double) y + d1, (double) z + d2, drop);
entityitem.delayBeforeCanPickup = 10;
player.worldObj.spawnEntityInWorld(entityitem);
}
stack.damageItem(1, player);
player.addStat(StatList.mineBlockStatArray[Block.getIdFromBlock(block)], 1);
}
}
return false;
}
@Override
public void initParticle(Particle p, Random r) {
float angle = mMinAngle;
if (mMaxAngle != mMinAngle) {
angle = r.nextInt(mMaxAngle - mMinAngle) + mMinAngle;
}
float angleInRads = (float) (angle*Math.PI/180f);
float value = r.nextFloat()*(mMaxValue-mMinValue)+mMinValue;
p.mAccelerationX = (float) (value * Math.cos(angleInRads));
p.mAccelerationY = (float) (value * Math.sin(angleInRads));
}
@Override
public void onUpdateNavigation(){
if(isGoingToTeleport()) {
if(teleportCounter == 0 || teleportCounter == 60) {
NetworkHandler.sendToAllAround(new PacketPlaySound(Sounds.HUD_INIT, pathfindingEntity.posX, pathfindingEntity.posY, pathfindingEntity.posZ, 0.1F, teleportCounter == 0 ? 0.7F : 1F, true), pathfindingEntity.worldObj);
}
if(teleportCounter < TELEPORT_TICKS - 40) {
Random rand = pathfindingEntity.getRNG();
float f = (rand.nextFloat() - 0.5F) * 0.02F * teleportCounter;
float f1 = (rand.nextFloat() - 0.5F) * 0.02F * teleportCounter;
float f2 = (rand.nextFloat() - 0.5F) * 0.02F * teleportCounter;
NetworkHandler.sendToAllAround(new PacketSpawnParticle("portal", pathfindingEntity.posX, pathfindingEntity.posY, pathfindingEntity.posZ, f, f1, f2), pathfindingEntity.worldObj);
}
if(++teleportCounter > TELEPORT_TICKS) {
if(pathfindingEntity.isBlockValidPathfindBlock(telX, telY, telZ)) {
teleport();
}
teleportCounter = -1;
setPath(null, 0);
pathfindingEntity.getMoveHelper().setMoveTo(telX, telY, telZ, pathfindingEntity.getSpeed());
pathfindingEntity.addAir(null, -10000);
}
} else {
super.onUpdateNavigation();
}
}
public void update(float delta, Viewport viewport){
Random random = new Random();
if (random.nextFloat() < delta * SPAWNS_PER_SECOND){
boulders.add(new Boulder(viewport));
}
for (int i = 0; i < boulders.size; i++){
Boulder boulder = boulders.get(i);
boulder.update(delta);
if (boulder.isBelowScreen()){
boulders.removeIndex(i);
}
}
}
public static long randomLong(Random r, long start, long end) {
return (long) (start + (r.nextFloat() * (end - start)));
}
@Override
public void initParticle(Particle p, Random r) {
p.mSpeedX = r.nextFloat()*(mMaxSpeedX-mMinSpeedX)+mMinSpeedX;
p.mSpeedY = r.nextFloat()*(mMaxSpeedY-mMinSpeedY)+mMinSpeedY;
}
@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) {
return rand.nextFloat() < 0.5;
}
@Override
protected Similarity getSimilarity(Random random) {
// term frequency normalization parameter k1
final float k1;
switch (random.nextInt(4)) {
case 0:
// minimum value
k1 = 0;
break;
case 1:
// tiny value
k1 = Float.MIN_VALUE;
break;
case 2:
// maximum value
// upper bounds on individual term's score is 43.262806 * (k1 + 1) * boost
// we just limit the test to "reasonable" k1 values but don't enforce this anywhere.
k1 = Integer.MAX_VALUE;
break;
default:
// random value
k1 = Integer.MAX_VALUE * random.nextFloat();
break;
}
// length normalization parameter b [0 .. 1]
final float b;
switch (random.nextInt(4)) {
case 0:
// minimum value
b = 0;
break;
case 1:
// tiny value
b = Float.MIN_VALUE;
break;
case 2:
// maximum value
b = 1;
break;
default:
// random value
b = random.nextFloat();
break;
}
return new BM25Similarity(k1, b);
}