本文整理汇总了Java中java.util.Random类的典型用法代码示例。如果您正苦于以下问题:Java Random类的具体用法?Java Random怎么用?Java Random使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Random类属于java.util包,在下文中一共展示了Random类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setImgBackGround
import java.util.Random; //导入依赖的package包/类
private void setImgBackGround() {
String[] images = getResources().getStringArray(R.array.splash_background);
int position = new Random().nextInt(images.length - 1) % (images.length);
Glide.with(this)
.load(images[position])
.into(mImgBackGround);
String path = SpUtil.getString(this, "path");
Glide.with(this)
.load(path)
.asBitmap()
.error(R.drawable.def_avatar)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mCircleImageView.setImageBitmap(resource);
}
});
//设置图像的透明度
AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(800);
mCircleImageView.startAnimation(animation);
animation.setAnimationListener(this);
}
示例2: generateSurface
import java.util.Random; //导入依赖的package包/类
private void generateSurface(World world, Random random, int startX, int startZ) {
/*
* if(startingX == 0 && startingZ == 0) { startingX = startX; startingZ = startZ; }
*
* if((startX > (startingX + 512)) || (startX < (startingX - 512))) { xWasBad = true; return; } else if(xWasBad) { if(!((startX > (startingX + 512)) || (startX < (startingX - 512)))) { FMLLog.log(Level.INFO,
* "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); startingX = startX; xWasBad = false; } }
*
* if((startZ > (startingZ + 512)) || (startZ < (startingZ - 512))) { zWasBad = true; return; } else if(zWasBad) { if(!((startZ > (startingZ + 512)) || (startZ < (startingZ - 512)))) { FMLLog.log(Level.INFO,
* "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"); startingZ = startZ; zWasBad = false; } }
*
* /* startingX = 0; startingZ = 0;
*/
for(int x = startX; x < startX + 16; x++) {
for(int z = startZ; z < startZ + 16; z++) {
removeStone(world, random, x, z);
}
}
addOresSurface(world, random, startX, startZ);
}
示例3: testConstructorsAllEqual
import java.util.Random; //导入依赖的package包/类
@Test
public void testConstructorsAllEqual() {
Random r = new Random();
int x = (int)(r.nextFloat()*100);
int y = (int)(r.nextFloat()*100);
int z = (int)(r.nextFloat()*100);
Point3 p = new Point3(x,y,z);
Annotation[] ans = new Annotation[4];
ans[0] = new Annotation(x,y,z);
ans[1] = new Annotation(p);
ans[2] = new Annotation(x,y,z,"");
ans[3] = new Annotation(p,"");
for (int i=0; i<ans.length; ++i) {
assertEquals(Color.WHITE,ans[i].getColor());
assertEquals(p,ans[i].getLocation());
assertEquals("",ans[i].getText());
assertEquals(Annotation.Alignment.EAST,ans[i].getAlignment());
assertEquals(
new Font("SansSerif",Font.PLAIN,18),ans[i].getFont());
}
}
示例4: Particle
import java.util.Random; //导入依赖的package包/类
protected Particle(World worldIn, double posXIn, double posYIn, double posZIn)
{
this.boundingBox = EMPTY_AABB;
this.width = 0.6F;
this.height = 1.8F;
this.rand = new Random();
this.particleAlpha = 1.0F;
this.worldObj = worldIn;
this.setSize(0.2F, 0.2F);
this.setPosition(posXIn, posYIn, posZIn);
this.prevPosX = posXIn;
this.prevPosY = posYIn;
this.prevPosZ = posZIn;
this.particleRed = 1.0F;
this.particleGreen = 1.0F;
this.particleBlue = 1.0F;
this.particleTextureJitterX = this.rand.nextFloat() * 3.0F;
this.particleTextureJitterY = this.rand.nextFloat() * 3.0F;
this.particleScale = (this.rand.nextFloat() * 0.5F + 0.5F) * 2.0F;
this.particleMaxAge = (int)(4.0F / (this.rand.nextFloat() * 0.9F + 0.1F));
this.particleAge = 0;
this.canCollide = true;
}
示例5: generatePassword
import java.util.Random; //导入依赖的package包/类
@Override
public String generatePassword(String id) {
if (id == null || id.isEmpty()) {
return null;
}
String password = null;
// Take the first 4 letters of id
if (id.length() <= 4) {
password = id;
} else {
password = id.substring(0, 4);
}
// Generate a 3 digit random number
Random r = new Random();
r.setSeed(System.currentTimeMillis());
float f = r.nextFloat();
password += String.valueOf((int) ((f * 1000.0f) % 1000));
return password;
}
示例6: generateChestContents
import java.util.Random; //导入依赖的package包/类
protected boolean generateChestContents(World worldIn, StructureBoundingBox boundingBoxIn, Random rand, int x, int y, int z, List<WeightedRandomChestContent> listIn, int max)
{
BlockPos blockpos = new BlockPos(this.getXWithOffset(x, z), this.getYWithOffset(y), this.getZWithOffset(x, z));
if (boundingBoxIn.isVecInside(blockpos) && worldIn.getBlockState(blockpos).getBlock().getMaterial() == Material.air)
{
int i = rand.nextBoolean() ? 1 : 0;
worldIn.setBlockState(blockpos, Blocks.rail.getStateFromMeta(this.getMetadataWithOffset(Blocks.rail, i)), 2);
EntityMinecartChest entityminecartchest = new EntityMinecartChest(worldIn, (double)((float)blockpos.getX() + 0.5F), (double)((float)blockpos.getY() + 0.5F), (double)((float)blockpos.getZ() + 0.5F));
WeightedRandomChestContent.generateChestContents(rand, listIn, entityminecartchest, max);
worldIn.spawnEntityInWorld(entityminecartchest);
return true;
}
else
{
return false;
}
}
示例7: func_180711_a
import java.util.Random; //导入依赖的package包/类
public void func_180711_a(World worldIn, Random p_180711_2_, BlockPos p_180711_3_)
{
this.func_175933_b(worldIn, p_180711_3_.west().north());
this.func_175933_b(worldIn, p_180711_3_.east(2).north());
this.func_175933_b(worldIn, p_180711_3_.west().south(2));
this.func_175933_b(worldIn, p_180711_3_.east(2).south(2));
for (int i = 0; i < 5; ++i)
{
int j = p_180711_2_.nextInt(64);
int k = j % 8;
int l = j / 8;
if (k == 0 || k == 7 || l == 0 || l == 7)
{
this.func_175933_b(worldIn, p_180711_3_.add(-3 + k, 0, -3 + l));
}
}
}
示例8: generate
import java.util.Random; //导入依赖的package包/类
public boolean generate(World worldIn, Random rand, BlockPos position)
{
this.world = worldIn;
this.basePos = position;
this.rand = new Random(rand.nextLong());
if (this.heightLimit == 0)
{
this.heightLimit = 5 + this.rand.nextInt(this.heightLimitLimit);
}
if (!this.validTreeLocation())
{
return false;
}
else
{
this.generateLeafNodeList();
this.generateLeaves();
this.generateTrunk();
this.generateLeafNodeBases();
return true;
}
}
示例9: scaleCodeHeapSize
import java.util.Random; //导入依赖的package包/类
/**
* Returns {@code unscaledSize} value scaled by a random factor from
* range (1, 2). If {@code unscaledSize} is not 0, then this
* method will return value that won't be equal to {@code unscaledSize}.
*
* @param unscaledSize The value to be scaled.
* @return {@code unscaledSize} value scaled by a factor from range (1, 2).
*/
private static long scaleCodeHeapSize(long unscaledSize) {
Random random = Utils.getRandomInstance();
long scaledSize = unscaledSize;
while (scaledSize == unscaledSize && unscaledSize != 0) {
float scale = 1.0f + random.nextFloat();
scaledSize = (long) Math.ceil(scale * unscaledSize);
}
return scaledSize;
}
示例10: generateScenario
import java.util.Random; //导入依赖的package包/类
@Override
public void generateScenario(MappingScenario scenario,Configuration configuration) throws Exception
{
Random _generator = configuration.getRandomGenerator();
Map<String, Map<String,FD>> fds = new HashMap<String, Map<String,FD>> ();
if (log.isDebugEnabled()) {log.debug("Attempting to Generate Source FDs");};
// create PK FDs R(A,B,C) if A is key then add A -> B,C
if (configuration.getParam(ParameterName.PrimaryKeyFDs) == 1) {
if (log.isDebugEnabled()) {log.debug("Generating PK FDs for those Relations with PKs");};
generatePKFDs(scenario, fds);
}
if (configuration.getParam(Constants.ParameterName.SourceFDPerc) > 0) {
if (log.isDebugEnabled()) {log.debug("Generating Random FDs as SourceFDPerc > 0");};
generateRandomFDs(scenario, configuration, _generator, fds);
}
}
示例11: addComponentParts
import java.util.Random; //导入依赖的package包/类
public boolean addComponentParts(World worldIn, Random randomIn, StructureBoundingBox structureBoundingBoxIn)
{
if (this.field_143015_k < 0)
{
this.field_143015_k = this.getAverageGroundLevel(worldIn, structureBoundingBoxIn);
if (this.field_143015_k < 0)
{
return true;
}
this.boundingBox.offset(0, this.field_143015_k - this.boundingBox.maxY + 4 - 1, 0);
}
this.fillWithBlocks(worldIn, structureBoundingBoxIn, 0, 0, 0, 2, 3, 1, Blocks.air.getDefaultState(), Blocks.air.getDefaultState(), false);
this.setBlockState(worldIn, Blocks.oak_fence.getDefaultState(), 1, 0, 0, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.oak_fence.getDefaultState(), 1, 1, 0, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.oak_fence.getDefaultState(), 1, 2, 0, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.wool.getStateFromMeta(EnumDyeColor.WHITE.getDyeDamage()), 1, 3, 0, structureBoundingBoxIn);
boolean flag = this.coordBaseMode == EnumFacing.EAST || this.coordBaseMode == EnumFacing.NORTH;
this.setBlockState(worldIn, Blocks.torch.getDefaultState().withProperty(BlockTorch.FACING, this.coordBaseMode.rotateY()), flag ? 2 : 0, 3, 0, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.torch.getDefaultState().withProperty(BlockTorch.FACING, this.coordBaseMode), 1, 3, 1, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.torch.getDefaultState().withProperty(BlockTorch.FACING, this.coordBaseMode.rotateYCCW()), flag ? 0 : 2, 3, 0, structureBoundingBoxIn);
this.setBlockState(worldIn, Blocks.torch.getDefaultState().withProperty(BlockTorch.FACING, this.coordBaseMode.getOpposite()), 1, 3, -1, structureBoundingBoxIn);
return true;
}
示例12: testLocalDateRowSortByKeys
import java.util.Random; //导入依赖的package包/类
@Test(dataProvider = "order")
public void testLocalDateRowSortByKeys(boolean ascending, boolean parallel) {
final Random random = new Random();
final Array<LocalDate> rowKeys = Range.ofLocalDates("2000-01-01", "2010-01-01").toArray().shuffle(2);
final Array<String> colKeys = Array.of("A", "B", "C", "D");
final DataFrame<LocalDate,String> frame = DataFrame.ofDoubles(rowKeys, colKeys).applyDoubles(v -> random.nextDouble());
frame.out().print();
final DataFrame<LocalDate,String> sorted = parallel ? frame.rows().parallel().sort(ascending) : frame.rows().sequential().sort(ascending);
sorted.out().print();
sorted.rows().forEach(row -> {
if (row.ordinal() > 0) {
final LocalDate key0 = sorted.rows().key(row.ordinal()-1);
final LocalDate key1 = sorted.rows().key(row.ordinal());
if (ascending) {
Assert.assertTrue(key0.compareTo(key1) < 0, "Keys are in ascending order");
} else {
Assert.assertTrue(key0.compareTo(key1) > 0, "Keys are in descending order");
}
}
});
}
示例13: initRNGs
import java.util.Random; //导入依赖的package包/类
private void initRNGs()
{
// Initialise a RNG for this scene:
long seed = 0;
if (this.mazeParams.getSeed() == null || this.mazeParams.getSeed().equals("random"))
seed = System.currentTimeMillis();
else
seed = Long.parseLong(this.mazeParams.getSeed());
this.pathrand = new Random(seed);
this.blockrand = new Random(seed);
// Should we initialise a separate RNG for the block types?
if (this.mazeParams.getMaterialSeed() != null)
{
long bseed = 0;
if (this.mazeParams.getMaterialSeed().equals("random"))
bseed = System.currentTimeMillis();
else
bseed = Long.parseLong(this.mazeParams.getMaterialSeed());
this.blockrand = new Random(bseed);
}
}
示例14: createTwoVolumesWithError
import java.util.Random; //导入依赖的package包/类
/**
* Cannot create two volumes with same name
*
* @throws Exception
*/
@Test()
public void createTwoVolumesWithError()
throws Exception {
VolumeResource volumeResource = new VolumeResource();
volumeResource.setName("shared" + new Random().nextInt(100000));
ResultActions resultActions = createVolume(volumeResource);
resultActions.andExpect(status().isCreated());
// Convert the result to pojo
volumeResource = getVolumeResource(mapper, resultActions);
// Try to create a second volume but error
resultActions = createVolume(volumeResource);
resultActions.andExpect(status().is4xxClientError());
// delete the volume
deleteVolume(volumeResource.getId(), HttpStatus.NO_CONTENT);
}
示例15: fillInventory
import java.util.Random; //导入依赖的package包/类
public void fillInventory(IInventory inventory, Random rand, LootContext context)
{
List<ItemStack> list = this.generateLootForPools(rand, context);
List<Integer> list1 = this.getEmptySlotsRandomized(inventory, rand);
this.shuffleItems(list, list1.size(), rand);
for (ItemStack itemstack : list)
{
if (list1.isEmpty())
{
LOGGER.warn("Tried to over-fill a container");
return;
}
if (itemstack == null)
{
inventory.setInventorySlotContents(((Integer)list1.remove(list1.size() - 1)).intValue(), (ItemStack)null);
}
else
{
inventory.setInventorySlotContents(((Integer)list1.remove(list1.size() - 1)).intValue(), itemstack);
}
}
}