本文整理汇总了Java中java.util.Random.nextBoolean方法的典型用法代码示例。如果您正苦于以下问题:Java Random.nextBoolean方法的具体用法?Java Random.nextBoolean怎么用?Java Random.nextBoolean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Random
的用法示例。
在下文中一共展示了Random.nextBoolean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testWithBooleans
import java.util.Random; //导入方法依赖的package包/类
@Test(dataProvider = "sizes")
public void testWithBooleans(int initialSize) {
final Random random = new Random();
final ArrayBuilder<Boolean> builder = ArrayBuilder.of(initialSize);
final boolean[] expected = new boolean[1000];
for (int i=0; i<expected.length; ++i) {
expected[i] = random.nextBoolean();
builder.add(expected[i]);
}
final Array<Boolean> actual = builder.toArray();
Assert.assertEquals(actual.length(), expected.length, "The lengths match");
Assert.assertEquals(actual.typeCode(), ArrayType.BOOLEAN, "The array type is as expected");
for (int i=0; i<expected.length; ++i) {
Assert.assertEquals(actual.getBoolean(i), expected[i], "The values match at " + i);
}
}
示例2: generateRandom
import java.util.Random; //导入方法依赖的package包/类
private String generateRandom(int len)
{
StringBuilder sb = new StringBuilder(len+1000); // pad for surrogates
Random r = new Random(len);
for (int i = 0; i < len; ++i) {
if (r.nextBoolean()) { // non-ascii
int value = r.nextInt() & 0xFFFF;
// Otherwise easy, except that need to ensure that
// surrogates are properly paired: and, also
// their values do not exceed 0x10FFFF
if (value >= 0xD800 && value <= 0xDFFF) {
// Let's discard first value, then, and produce valid pair
int fullValue = (r.nextInt() & 0xFFFFF);
sb.append((char) (0xD800 + (fullValue >> 10)));
value = 0xDC00 + (fullValue & 0x3FF);
}
sb.append((char) value);
} else { // ascii
sb.append((char) (r.nextInt() & 0x7F));
}
}
return sb.toString();
}
示例3: doubleArrays
import java.util.Random; //导入方法依赖的package包/类
@Test public void doubleArrays() throws Exception {
final Random r = new Random();
try (Connection conn = DriverManager.getConnection(url)) {
ScalarType component = ColumnMetaData.scalar(Types.DOUBLE, "DOUBLE", Rep.DOUBLE);
List<Array> arrays = new ArrayList<>();
// Construct the data
for (int i = 0; i < 3; i++) {
List<Double> elements = new ArrayList<>();
for (int j = 0; j < 7; j++) {
double element = r.nextDouble();
if (r.nextBoolean()) {
element *= -1;
}
elements.add(element);
}
arrays.add(createArray("DOUBLE", component, elements));
}
writeAndReadArrays(conn, "float_arrays", "DOUBLE", component, arrays,
PRIMITIVE_LIST_VALIDATOR);
}
}
示例4: run
import java.util.Random; //导入方法依赖的package包/类
@Override
protected void run(Context context) throws Exception {
Random random = context.container().random();
int actionIndex = random.nextInt(4);
boolean select = random.nextBoolean();
int direction;
switch (actionIndex) {
case 0:
direction = SwingConstants.WEST;
break;
case 1:
direction = SwingConstants.EAST;
break;
case 2:
direction = SwingConstants.NORTH;
break;
case 3:
direction = SwingConstants.SOUTH;
break;
default:
throw new IllegalStateException("Invalid actionIndex=" + actionIndex); // NOI18N
}
moveOrSelect(context, direction, select);
}
示例5: generate
import java.util.Random; //导入方法依赖的package包/类
public RoomData generate(RegionHandler regionHandler, Random rand, int x, int y, int minSize, int maxSize)
{
int size = (minSize + rand.nextInt(maxSize)) * 2 + 1;
int rectable = rand.nextInt(1 + size / 2) * 2;
int width = size;
int height = size;
if (rand.nextBoolean())
{
width += rectable;
}
else
{
height += rectable;
}
x += rand.nextInt((this.width - width) / 2) * 2 + 1;
y += rand.nextInt((this.height - height) / 2) * 2 + 1;
RoomData room = new RoomData(regionHandler.getNextAvailableRegion(), x, y, width - 1, height - 1);
return room;
}
示例6: getRandDisplacement
import java.util.Random; //导入方法依赖的package包/类
private float getRandDisplacement(Random rand)
{
float ret = rand.nextFloat();
//Restrict the amount of displacement possible
ret = MathHelper.clamp(ret, -0.1F, 0.1F);
return rand.nextBoolean() ? ret : -ret;
}
示例7: WoodHut
import java.util.Random; //导入方法依赖的package包/类
public WoodHut(StructureVillagePieces.Start start, int p_i45565_2_, Random rand, StructureBoundingBox p_i45565_4_, EnumFacing facing)
{
super(start, p_i45565_2_);
this.coordBaseMode = facing;
this.boundingBox = p_i45565_4_;
this.isTallHouse = rand.nextBoolean();
this.tablePosition = rand.nextInt(3);
}
示例8: testNextBoolean
import java.util.Random; //导入方法依赖的package包/类
/**
* Repeated calls to nextBoolean produce at least two distinct results
*/
public void testNextBoolean() {
Random r = new Random();
boolean f = r.nextBoolean();
int i = 0;
while (i < NCALLS && r.nextBoolean() == f)
++i;
assertTrue(i < NCALLS);
}
示例9: getRandom
import java.util.Random; //导入方法依赖的package包/类
/**
* Create a random vector with a magnitude no greater than specified
* @param rand The source of randomness to use.
* @param maxMagnitude
* @return A new random Vector2D
*/
public static Vector2D getRandom(Random rand, double maxMagnitude) {
final double max = maxMagnitude * maxMagnitude;
final double x2 = rand.nextDouble() * max;
final double y2 = rand.nextDouble() * (max - x2);
final double x = rand.nextBoolean() ? (double)Math.sqrt(x2) : -(double)Math.sqrt(x2);
final double y = rand.nextBoolean() ? (double)Math.sqrt(y2) : -(double)Math.sqrt(y2);
return new Vector2D(x, y);
}
示例10: WoodHut
import java.util.Random; //导入方法依赖的package包/类
public WoodHut(StructureVillagePieces.Start start, int type, Random rand, StructureBoundingBox structurebb, EnumFacing facing)
{
super(start, type);
this.setCoordBaseMode(facing);
this.boundingBox = structurebb;
this.isTallHouse = rand.nextBoolean();
this.tablePosition = rand.nextInt(3);
}
示例11: generoiTulokset
import java.util.Random; //导入方法依赖的package包/类
private GuessingGameTest.BinaariHaunTulokset generoiTulokset(int ala, int yla, Boolean vastausAina) {
Random random = new Random();
int nykyinenAla = ala;
int nykyinenYla = yla;
List<String> vastaukset = new ArrayList<String>();
List<Integer> kysytytLuvut = new ArrayList<Integer>();
while (nykyinenAla < nykyinenYla) {
int puolivali = (nykyinenAla + nykyinenYla) / 2;
kysytytLuvut.add(puolivali);
boolean suurempi;
if (vastausAina != null) {
suurempi = vastausAina.booleanValue();
} else {
suurempi = random.nextBoolean();
}
if (suurempi) {
vastaukset.add("y");
nykyinenAla = puolivali + 1;
} else {
vastaukset.add("n");
nykyinenYla = puolivali;
}
}
return new GuessingGameTest.BinaariHaunTulokset(nykyinenAla, vastaukset, kysytytLuvut);
}
示例12: generateRandom
import java.util.Random; //导入方法依赖的package包/类
@Override
protected Boolean generateRandom(Random ran) {
return ran.nextBoolean();
}
示例13: _testUtf8StringValue
import java.util.Random; //导入方法依赖的package包/类
public void _testUtf8StringValue(int mode) throws Exception
{
Random r = new Random(13);
//int LEN = 72000;
int LEN = 720;
StringBuilder sb = new StringBuilder(LEN + 20);
while (sb.length() < LEN) {
int c;
if (r.nextBoolean()) { // ascii
c = 32 + (r.nextInt() & 0x3F);
if (c == '"' || c == '\\') {
c = ' ';
}
} else if (r.nextBoolean()) { // 2-byte
c = 160 + (r.nextInt() & 0x3FF);
} else if (r.nextBoolean()) { // 3-byte (non-surrogate)
c = 8000 + (r.nextInt() & 0x7FFF);
} else { // surrogates (2 chars)
int value = r.nextInt() & 0x3FFFF; // 20-bit, ~ 1 million
sb.append((char) (0xD800 + (value >> 10)));
c = (0xDC00 + (value & 0x3FF));
}
sb.append((char) c);
}
ByteArrayOutputStream bout = new ByteArrayOutputStream(LEN);
OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8");
out.write("[\"");
String VALUE = sb.toString();
out.write(VALUE);
out.write("\"]");
out.close();
byte[] data = bout.toByteArray();
JsonParser p = createParser(mode, data);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
String act = p.getText();
assertEquals(VALUE.length(), act.length());
assertEquals(VALUE, act);
p.close();
}
示例14: shouldImproveEnchantment
import java.util.Random; //导入方法依赖的package包/类
private static boolean shouldImproveEnchantment(Random rand, Enchantment enchantment, Integer level) {
return level < enchantment.getMaxLevel() && rand.nextBoolean();
}
示例15: generateMini
import java.util.Random; //导入方法依赖的package包/类
private boolean generateMini(World worldIn, Random rand, BlockPos position) {
int i = rand.nextInt(2) + 6;
boolean flag = true;
if (position.getY() >= 1 && position.getY() + i + 1 <= worldIn.getHeight()) {
flag = isAreaClear(worldIn, position, i);
if (!flag) {
return false;
}
//If we can actually make the tree
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());
//MAKE LEAVES
final int[][] leafPos = new int[][] { { -1, 0, 1 }, { -1, 0, 0 }, { -1, 0, -1 }, { 0, 0, 1 }, { 0, 0, -1 }, { 1, 0, 1 }, { 1, 0, 0 }, { 1, 0, -1 }, { -1, 1, 1 }, { -1, 1, 0 }, { -1, 1, -1 }, { 0, 1, 1 }, { 0, 1, -1 }, { 0, 1, -2 }, { 1, 1, 1 }, { 1, 1, 0 }, { 1, 1, -1 }, { -1, 1, 0 }, { -1, 1, -1 }, { -2, 1, 1 }, { -2, 1, 0 }, { -2, 1, -1 }, { 2, 1, -1 }, { 2, 1, 0 }, { 2, 1, 1 }, { -1, 1, 2 }, { 0, 1, 2 }, { 1, 1, 2 }, { -1, 1, -2 }, { 1, 1, -2 }, { 1, 1, -2 }, { -1, 2, 1 }, { -1, 2, 0 }, { -1, 2, -1 }, { 0, 2, 1 }, { 0, 2, -1 }, { 1, 2, 1 }, { 1, 2, 0 }, { 1, 2, -1 }, { -2, 2, 0 }, { 2, 2, 0 }, { 0, 2, 2 }, { 0, 2, -2 }, { -1, 3, 1 }, { -1, 3, 0 }, { -1, 3, -1 }, { 0, 3, 1 }, { 0, 3, -1 }, { 1, 3, 1 }, { 1, 3, 0 }, { 1, 3, -1 }, { -1, 4, 0 }, { 0, 4, 1 },
{ 0, 4, -1 }, { 1, 4, 0 }, { 0, 5, 0 } };
for (int[] coord : leafPos) {
BlockPos blockpos = position.add(coord[0], coord[1] + (i - 5), coord[2]);
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);
}
}
//MAKE TRUNK
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);
if (j3 == i - 4) {
this.setBlockAndNotifyAdequately(worldIn, position.up(j3).north(), this.metaWood.withProperty(BlockLog.LOG_AXIS, BlockLog.EnumAxis.Z));
this.setBlockAndNotifyAdequately(worldIn, position.up(j3).south(), this.metaWood.withProperty(BlockLog.LOG_AXIS, BlockLog.EnumAxis.Z));
if (rand.nextBoolean()) {
this.setBlockAndNotifyAdequately(worldIn, position.up(j3).east(), this.metaWood.withProperty(BlockLog.LOG_AXIS, BlockLog.EnumAxis.X));
this.setBlockAndNotifyAdequately(worldIn, position.up(j3).west(), this.metaWood.withProperty(BlockLog.LOG_AXIS, BlockLog.EnumAxis.X));
}
}
}
}
return true;
} else {
return false;
}
}
} else {
return false;
}
}