Java Code Examples for java.util.Random.nextInt()

The following are Jave code examples for showing how to use nextInt() of the java.util.Random class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: StarrySkyDemo   File: StarrySkyView.java   Source Code and License Vote up 6 votes
/**
 * 初始化星球运行方向
 */
private int getStarDirection() {
    Random random = new Random();
    int randomInt = random.nextInt(4);
    int direction = 0;
    switch (randomInt) {
        case 0:
            direction = LEFT;
            break;
        case 1:
            direction = RIGHT;
            break;
        case 2:
            direction = TOP;
            break;
        case 3:
            direction = BOTTOM;
            break;
    }
    return direction;
}
 
Example 2
Project: guava-mock   File: ConcurrentHashMultisetBenchmark.java   Source Code and License Vote up 6 votes
private long runAddRemoveSingleThread(int reps) {
  Random random = new Random();
  int nKeys = keys.size();
  long blah = 0;
  for (int i = 0; i < reps; i++) {
    Integer key = keys.get(random.nextInt(nKeys));
    // This range is [-5, 4] - slight negative bias so we often hit zero, which brings the
    // auto-removal of zeroes into play.
    int delta = random.nextInt(10) - 5;
    blah += delta;
    if (delta >= 0) {
      multiset.add(key, delta);
    } else {
      multiset.remove(key, -delta);
    }
  }
  return blah;
}
 
Example 3
Project: incubator-netbeans   File: DocumentContentTesting.java   Source Code and License Vote up 6 votes
@Override
protected void run(Context context) throws Exception {
    Document expectedDoc = getExpectedDocument(context);
    int docLen = expectedDoc.getLength();
    if (docLen == 0)
        return; // Nothing to possibly remove
    Random random = context.container().random();
    int length;
    if (REMOVE_CHAR.equals(name())) {
        length = 1;
    } else if (REMOVE_TEXT.equals(name())) {
        Integer maxLength = (Integer) context.getPropertyOrNull(REMOVE_TEXT_MAX_LENGTH);
        if (maxLength == null)
            maxLength = Integer.valueOf(10);
        if (maxLength > docLen)
            maxLength = Integer.valueOf(docLen);
        length = random.nextInt(maxLength) + 1;
    } else {
        throw new IllegalStateException("Unexpected op name=" + name());
    }
    int offset = random.nextInt(docLen - length + 1);
    remove(context, offset, length);
}
 
Example 4
Project: BaseClient   File: StructureStrongholdPieces.java   Source Code and License Vote up 5 votes
public Crossing(int p_i45580_1_, Random p_i45580_2_, StructureBoundingBox p_i45580_3_, EnumFacing p_i45580_4_)
{
    super(p_i45580_1_);
    this.coordBaseMode = p_i45580_4_;
    this.field_143013_d = this.getRandomDoor(p_i45580_2_);
    this.boundingBox = p_i45580_3_;
    this.field_74996_b = p_i45580_2_.nextBoolean();
    this.field_74997_c = p_i45580_2_.nextBoolean();
    this.field_74995_d = p_i45580_2_.nextBoolean();
    this.field_74999_h = p_i45580_2_.nextInt(3) > 0;
}
 
Example 5
Project: lazycat   File: UUIDGenerator.java   Source Code and License Vote up 5 votes
/**
 * Same as java.util.Random.nextBytes except this one we dont have to
 * allocate a new byte array
 * 
 * @param into
 *            byte[]
 * @param offset
 *            int
 * @param length
 *            int
 * @param r
 *            Random
 */
public static void nextBytes(byte[] into, int offset, int length, Random r) {
	int numRequested = length;
	int numGot = 0, rnd = 0;
	while (true) {
		for (int i = 0; i < BYTES_PER_INT; i++) {
			if (numGot == numRequested)
				return;
			rnd = (i == 0 ? r.nextInt() : rnd >> BITS_PER_BYTE);
			into[offset + numGot] = (byte) rnd;
			numGot++;
		}
	}
}
 
Example 6
Project: OneTwo   File: TouchDisplayView.java   Source Code and License Vote up 5 votes
private static void shuffleArray(int[] array)
{
    int index;
    Random random = new Random();
    for (int i = array.length - 1; i > 0; i--)
    {
        index = random.nextInt(i + 1);
        if (index != i)
        {
            array[index] ^= array[i];
            array[i] ^= array[index];
            array[index] ^= array[i];
        }
    }
}
 
Example 7
Project: FastWWSearch   File: Tools.java   Source Code and License Vote up 5 votes
/**
 * Generate random permutation given an array
 * @param array
 * @return
 */
public static final int[] randPermutation(int[] array) {
	Random r = new Random();
	int randNum;
	for (int i = 0; i < array.length; i++) {
		randNum = i + r.nextInt(array.length-i);
		swap(array, i, randNum);
	}
	return array;
}
 
Example 8
Project: YFHR_Android_App   File: SettingsActivity.java   Source Code and License Vote up 5 votes
public static String random() {
    Random generator = new Random();
    StringBuilder randomStringBuilder = new StringBuilder();
    int randomLength = generator.nextInt(20);
    char tempChar;
    for (int i = 0; i < randomLength; i++){
        tempChar = (char) (generator.nextInt(96) + 32);
        randomStringBuilder.append(tempChar);
    }
    return randomStringBuilder.toString();
}
 
Example 9
Project: SoftUni   File: Dummy.java   Source Code and License Vote up 5 votes
private void createListOfRandomWeaponsForLoot() {
    Random rnd = new Random();
    int maxLoot = 3 + rnd.nextInt(7);

    for (int i = 0; i < maxLoot; i++) {
        this.possibleLoot.add(new Axe(rnd.nextInt(50), rnd.nextInt(50)));
    }
}
 
Example 10
Project: word-clock-raspberry-pi-zero-neopixels   File: Tetris.java   Source Code and License Vote up 5 votes
public void renderNextTick(){
    if(!fall()){
        Random random = new Random();
        Brick[] enlargedField = new Brick[field.length+1];
        java.lang.System.arraycopy(field,0,enlargedField,0,enlargedField.length);
        enlargedField[enlargedField.length-1] = currentBrick.copy();
        field = enlargedField;
        currentBrick = nextBrick;
        nextBrick = bricks[random.nextInt(bricks.length)];
    }
}
 
Example 11
Project: BaseClient   File: StructureStrongholdPieces.java   Source Code and License Vote up 5 votes
public Straight(int p_i45573_1_, Random p_i45573_2_, StructureBoundingBox p_i45573_3_, EnumFacing p_i45573_4_)
{
    super(p_i45573_1_);
    this.coordBaseMode = p_i45573_4_;
    this.field_143013_d = this.getRandomDoor(p_i45573_2_);
    this.boundingBox = p_i45573_3_;
    this.expandsX = p_i45573_2_.nextInt(2) == 0;
    this.expandsZ = p_i45573_2_.nextInt(2) == 0;
}
 
Example 12
Project: Firma   File: TreeFir.java   Source Code and License Vote up 5 votes
@Override
public boolean generateTree(World w, Random rand, BlockPos pos) {
	filler.leaf=leaf;
	// Check light levels
	if(w.getLight(pos)<7){
		return false;
	}
	// Check height of trunk
	int h = 3+rand.nextInt(height);
	for(int y = 0; y<h; y++){
		if(!checkBlockAt(w,pos.add(0,y,0))){
			return false;
		}
	}
	for(int y = 0; y < h; y++){
		fill(w,pos.add(0,y,0));
	}
	for(int branch = 0; branch < 5; branch++){
		float incx = rand.nextFloat()-.5f;
		float incz = rand.nextFloat()-.5f;
		float incy = rand.nextFloat()*0.5f + 0.5f;
		float x=0,y=0,z=0;
		for(int bc = 0; bc < 5; bc++){
			x+=incx; y+=incy; z+=incz;
			BlockPos p = pos.add(x, y+h, z);
			fill(w,p);
			filler.fillLeaves(w, p, rand);
		}
	}
	return true;
}
 
Example 13
Project: phone-simulator   File: TestSmsClientMan.java   Source Code and License Vote up 4 votes
@Override
public void onForwardShortMessageRequest(ForwardShortMessageRequest ind) {
    if (!isStarted)
        return;

    MAPDialogSms curDialog = ind.getMAPDialog();
    long invokeId = ind.getInvokeId();
    SM_RP_DA da = ind.getSM_RP_DA();
    SM_RP_OA oa = ind.getSM_RP_OA();
    SmsSignalInfo si = ind.getSM_RP_UI();

    if (da.getIMSI() != null || da.getLMSI() != null) { // mt message
        this.onMtRequest(da, oa, si, curDialog);

        try {
            MtFSMReaction mtFSMReaction = this.testerHost.getConfigurationData().getTestSmsClientConfigurationData().getMtFSMReaction();

            Random rnd = new Random();
            if (this.testerHost.getConfigurationData().getTestSmsClientConfigurationData().isReturn20PersDeliveryErrors()) {
                int n = rnd.nextInt(5);
                if (n == 0) {
                    n = rnd.nextInt(5);
                    mtFSMReaction = new MtFSMReaction(n + 2);
                } else {
                    mtFSMReaction = new MtFSMReaction(MtFSMReaction.VAL_RETURN_SUCCESS);
                }
            }

            if (mtFSMReaction.intValue() == MtFSMReaction.VAL_RETURN_SUCCESS) {
                curDialog.addForwardShortMessageResponse(invokeId);
                this.countMtFsmResp++;

                if (!this.testerHost.getConfigurationData().getTestSmsClientConfigurationData().isOneNotificationFor100Dialogs()) {
                    this.testerHost.sendNotif(SOURCE_NAME, "Sent: mtResp", "", Level.DEBUG);
                }

                if (this.testerHost.getConfigurationData().getTestSmsClientConfigurationData().isContinueDialog())
                    this.needSendSend = true;
                else
                    this.needSendClose = true;
            } else {
                sendMtError(curDialog, invokeId, mtFSMReaction);
                this.needSendClose = true;
            }

        } catch (MAPException e) {
            this.testerHost.sendNotif(SOURCE_NAME, "Exception when invoking addMtForwardShortMessageResponse : " + e.getMessage(), e, Level.ERROR);
        }
    }
}
 
Example 14
Project: Backmemed   File: StructureStrongholdPieces.java   Source Code and License Vote up 4 votes
private static StructureStrongholdPieces.Stronghold generatePieceFromSmallDoor(StructureStrongholdPieces.Stairs2 p_175955_0_, List<StructureComponent> p_175955_1_, Random p_175955_2_, int p_175955_3_, int p_175955_4_, int p_175955_5_, EnumFacing p_175955_6_, int p_175955_7_)
{
    if (!canAddStructurePieces())
    {
        return null;
    }
    else
    {
        if (strongComponentType != null)
        {
            StructureStrongholdPieces.Stronghold structurestrongholdpieces$stronghold = findAndCreatePieceFactory(strongComponentType, p_175955_1_, p_175955_2_, p_175955_3_, p_175955_4_, p_175955_5_, p_175955_6_, p_175955_7_);
            strongComponentType = null;

            if (structurestrongholdpieces$stronghold != null)
            {
                return structurestrongholdpieces$stronghold;
            }
        }

        int j = 0;

        while (j < 5)
        {
            ++j;
            int i = p_175955_2_.nextInt(totalWeight);

            for (StructureStrongholdPieces.PieceWeight structurestrongholdpieces$pieceweight : structurePieceList)
            {
                i -= structurestrongholdpieces$pieceweight.pieceWeight;

                if (i < 0)
                {
                    if (!structurestrongholdpieces$pieceweight.canSpawnMoreStructuresOfType(p_175955_7_) || structurestrongholdpieces$pieceweight == p_175955_0_.strongholdPieceWeight)
                    {
                        break;
                    }

                    StructureStrongholdPieces.Stronghold structurestrongholdpieces$stronghold1 = findAndCreatePieceFactory(structurestrongholdpieces$pieceweight.pieceClass, p_175955_1_, p_175955_2_, p_175955_3_, p_175955_4_, p_175955_5_, p_175955_6_, p_175955_7_);

                    if (structurestrongholdpieces$stronghold1 != null)
                    {
                        ++structurestrongholdpieces$pieceweight.instancesSpawned;
                        p_175955_0_.strongholdPieceWeight = structurestrongholdpieces$pieceweight;

                        if (!structurestrongholdpieces$pieceweight.canSpawnMoreStructures())
                        {
                            structurePieceList.remove(structurestrongholdpieces$pieceweight);
                        }

                        return structurestrongholdpieces$stronghold1;
                    }
                }
            }
        }

        StructureBoundingBox structureboundingbox = StructureStrongholdPieces.Corridor.findPieceBox(p_175955_1_, p_175955_2_, p_175955_3_, p_175955_4_, p_175955_5_, p_175955_6_);

        if (structureboundingbox != null && structureboundingbox.minY > 1)
        {
            return new StructureStrongholdPieces.Corridor(p_175955_7_, p_175955_2_, structureboundingbox, p_175955_6_);
        }
        else
        {
            return null;
        }
    }
}
 
Example 15
Project: DecompiledMinecraft   File: WorldGenIcePath.java   Source Code and License Vote up 4 votes
public boolean generate(World worldIn, Random rand, BlockPos position)
{
    while (worldIn.isAirBlock(position) && position.getY() > 2)
    {
        position = position.down();
    }

    if (worldIn.getBlockState(position).getBlock() != Blocks.snow)
    {
        return false;
    }
    else
    {
        int i = rand.nextInt(this.basePathWidth - 2) + 2;
        int j = 1;

        for (int k = position.getX() - i; k <= position.getX() + i; ++k)
        {
            for (int l = position.getZ() - i; l <= position.getZ() + i; ++l)
            {
                int i1 = k - position.getX();
                int j1 = l - position.getZ();

                if (i1 * i1 + j1 * j1 <= i * i)
                {
                    for (int k1 = position.getY() - j; k1 <= position.getY() + j; ++k1)
                    {
                        BlockPos blockpos = new BlockPos(k, k1, l);
                        Block block = worldIn.getBlockState(blockpos).getBlock();

                        if (block == Blocks.dirt || block == Blocks.snow || block == Blocks.ice)
                        {
                            worldIn.setBlockState(blockpos, this.block.getDefaultState(), 2);
                        }
                    }
                }
            }
        }

        return true;
    }
}
 
Example 16
Project: ClashRoyale-Bot   File: Point.java   Source Code and License Vote up 4 votes
public Point(int random) {
	Random rand = new Random();
	x = rand.nextInt(random) - (random/2);
	y = rand.nextInt(random) - (random/2);
}
 
Example 17
Project: CloudLand-Server   File: ObjectOakTree.java   Source Code and License Vote up 4 votes
@Override
public void placeObject(ChunkManager map, int x, int y, int z, Random random) {
    this.treeHeight = random.nextInt(3) + 4;
    super.placeObject(map, x, y, z, random);
}
 
Example 18
Project: Got-Wood   File: WorldGenRubber.java   Source Code and License Vote up 4 votes
public boolean generate(World worldIn, Random rand, BlockPos position) {
	int i = 2 + rand.nextInt(1) + this.minTreeHeight;
	boolean flag = true;

	if (position.getY() >= 1 && position.getY() + i + 1 <= worldIn.getHeight()) {
		for (int j = position.getY(); j <= position.getY() + 1 + i; ++j) {
			int k = 1;

			if (j == position.getY()) {
				k = 0;
			}

			if (j >= position.getY() + 1 + i - 2) {
				k = 2;
			}

			BlockPos.MutableBlockPos blockposmutableblockpos = new BlockPos.MutableBlockPos();

			for (int l = position.getX() - k; l <= position.getX() + k && flag; ++l) {
				for (int i1 = position.getZ() - k; i1 <= position.getZ() + k && flag; ++i1) {
					if (j >= 0 && j < worldIn.getHeight()) {
						if (!this.isReplaceable(worldIn, blockposmutableblockpos.setPos(l, j, i1))) {
							flag = false;
						}
					} else {
						flag = false;
					}
				}
			}
		}

		if (!flag) {
			return false;
		} else {
			IBlockState state = worldIn.getBlockState(position.down());

			if (state.getBlock().canSustainPlant(state, worldIn, position.down(), net.minecraft.util.EnumFacing.UP, (net.minecraft.block.BlockSapling) net.minecraft.init.Blocks.SAPLING) && position.getY() < worldIn.getHeight() - i - 1) {
				this.setDirtAt(worldIn, position.down());

				for (int i3 = position.getY() - 3 + i; i3 <= position.getY() + i; ++i3) {
					int i4 = i3 - (position.getY() + i);
					int j1 = 1 - i4 / 2;

					for (int k1 = position.getX() - j1; k1 <= position.getX() + j1; ++k1) {
						int l1 = k1 - position.getX();

						for (int i2 = position.getZ() - j1; i2 <= position.getZ() + j1; ++i2) {
							int j2 = i2 - position.getZ();

							if (Math.abs(l1) != j1 || Math.abs(j2) != j1 || rand.nextInt(2) != 0 && i4 != 0) {
								BlockPos blockpos = new BlockPos(k1, i3, i2);
								state = worldIn.getBlockState(blockpos);

								if (state.getBlock().isAir(state, worldIn, blockpos) || state.getBlock().isLeaves(state, worldIn, blockpos) || state.getMaterial() == Material.VINE) {
									this.setBlockAndNotifyAdequately(worldIn, blockpos, this.metaLeaves);
								}
							}
						}
					}
				}

				for (int j3 = 0; j3 < i; ++j3) {
					BlockPos upN = position.up(j3);
					state = worldIn.getBlockState(upN);

					if (state.getBlock().isAir(state, worldIn, upN) || state.getBlock().isLeaves(state, worldIn, upN) || state.getMaterial() == Material.VINE) {
						this.setBlockAndNotifyAdequately(worldIn, position.up(j3), this.metaWood);
					}
				}

				return true;
			} else {
				return false;
			}
		}
	} else {
		return false;
	}
}
 
Example 19
Project: SamaGamesAPI   File: WinEffect.java   Source Code and License Vote up 4 votes
@Override
public void run()
{
    if (this.time < 20)
    {
        Firework fw = (Firework) player.getWorld().spawnEntity(player.getPlayer().getLocation(), EntityType.FIREWORK);
        FireworkMeta fwm = fw.getFireworkMeta();

        Random r = new Random();
        int rt = r.nextInt(4) + 1;

        FireworkEffect.Type type = FireworkEffect.Type.BALL;

        if (rt == 1)
            type = FireworkEffect.Type.BALL;
        else if (rt == 2)
            type = FireworkEffect.Type.BALL_LARGE;
        else if (rt == 3)
            type = FireworkEffect.Type.BURST;
        else if (rt == 4)
            type = FireworkEffect.Type.CREEPER;
        else if (rt == 5)
            type = FireworkEffect.Type.STAR;

        int r1i = r.nextInt(15) + 1;
        int r2i = r.nextInt(15) + 1;

        Color c1 = ColorUtils.getColor(r1i);
        Color c2 = ColorUtils.getColor(r2i);

        FireworkEffect effect = FireworkEffect.builder().flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(r.nextBoolean()).build();
        fwm.addEffect(effect);

        int rp = r.nextInt(2) + 1;
        fwm.setPower(rp);

        fw.setFireworkMeta(fwm);

        this.time++;
    }
}
 
Example 20
Project: jkes   File: KafkaConnectClient.java   Source Code and License Vote up 4 votes
private String getRandomUrl() {
    Random random = new Random();
    int index = random.nextInt(this.urls.length);
    return this.urls[index];
}