APISonar


java.util.Random.nextInt

> java > util > Random > nextInt
java APIs util APIs Random APIs nextInt APIs

Example 1
Random r;

  int rSz(int orderOfMagnitude) {
    int sz = r.nextInt(orderOfMagnitude);
    switch (sz) {
      case 0: return r.nextInt(10);
      case 1: return r.nextInt(100);
      case 2: return r.nextInt(1000);
      default: return r.nextInt(10000);
    }
  }
Example 2
public int example() {
    Random rng = new Random();
    int x = rng.nextInt();
    x = x + rng.nextInt() + 20;
    
    x = x + 5 + rng.nextInt(30);
    
    x = x + 20;
    // comments should block matching
    x = x + rng.nextInt();
    return x;
  }
Example 3
void shouldGiveSameResultEvenWhenArraysContainDifferentTypes()
    {
        int[] ints = new int[32];
        long[] longs = new long[32];

        Random r = ThreadLocalRandom.current();
        for ( int i = 0; i < 32; i++ )
        {
            int nextInt = r.nextInt();
            ints[i] = nextInt;
            longs[i] = nextInt;
        }

        assertThat( hash( ints ), equalTo( hash( longs ) ) );
    }
Example 4
public void testNextInt100() {
    Random r = new Random(1);
    assertEquals(85, r.nextInt(100));
    assertEquals(88, r.nextInt(100));
    assertEquals(47, r.nextInt(100));
    assertEquals(13, r.nextInt(100));
    assertEquals(54, r.nextInt(100));
  }
Example 5
public void testPartitionableStateRepartitioning() {
		Random r = new Random(42);

		for (int run = 0; run < 10000; ++run) {
			int oldParallelism = 1 + r.nextInt(9);
			int newParallelism = 1 + r.nextInt(9);

			int numNamedStates = 1 + r.nextInt(9);
			int maxPartitionsPerState = 1 + r.nextInt(9);

			doTestPartitionableStateRepartitioning(
					r, oldParallelism, newParallelism, numNamedStates, maxPartitionsPerState);
		}
	}
Example 6
public static Box createRandomBox() {
		Random r = new Random();
		return new Box(r.nextInt(100), r.nextInt(100), r.nextInt(100));
	}
Example 7
private final Random r;

  public void close() throws IOException {
    if (r.nextInt(4) == 2) {
      w.optimize();
    }
    w.close();
  }
Example 8
private Random random;

	public ItemWriter<Integer> itemWriter() {
		return items -> {
			for (Integer item : items) {
				int nextInt = random.nextInt(1000);
				Thread.sleep(nextInt);
				// simulate write failure
				if (nextInt % 57 == 0) {
					throw new Exception("Boom!");
				}
				System.out.println("item = " + item);
			}
		};
	}
Example 9
public static Integer getPostalCode(Random r) {
        int n = r.nextInt(100000);
        if (n < 10000) {
            n += 10000;
        }
        return n;
    }
Example 10
protected String genIpAddress() {
        Random rand = new Random();
        String address = rand.nextInt(255) + "." + rand.nextInt(255) + "." + rand.nextInt(255) + "." + rand.nextInt(255);
        return address;
    }
Example 11
public BasicRecord(final Random r) {
    f1 = r.nextDouble();
    f2 = r.nextDouble();
    f3 = r.nextDouble();
    f4 = r.nextInt();
    f5 = r.nextInt();
    f6 = r.nextInt();
  }
Example 12
private final Random randomGenerator;

	public Integer next() {
		currentCount++;
		return randomGenerator.nextInt();
	}
Example 13
public static void fuzz(Random r, Address a) {
    a.setCity(CITY_NAMES[r.nextInt(CITY_NAMES.length)]);
    a.setStreet(r.nextInt(4096) + " "
        + STREET_NAMES[r.nextInt(STREET_NAMES.length)]);
    a.setState(STATE_NAMES[r.nextInt(STATE_NAMES.length)]);
    StringBuilder zip = new StringBuilder();
    zip.append(String.format("%05d", r.nextInt(99999)));
    if (r.nextBoolean()) {
      zip.append(String.format("-%04d", r.nextInt(9999)));
    }
    a.setZip(zip.toString());
  }
Example 14
private byte[] createRandomBytes( Random r )
    {
        return new byte[r.nextInt( 1024 )];
    }
Example 15
private String randomString(Random r) {
    char c = (char) ('a' + r.nextInt(26));
    return "" + c;
  }
Example 16
private Random r;

    public void test_nextInt() {
        // Test for method int java.util.Random.nextInt()
        int lastNum = r.nextInt();
        int nextNum;
        boolean someDifferent = false;
        for (int counter = 0; counter < 100; counter++) {
            nextNum = r.nextInt();
            if (nextNum != lastNum)
                someDifferent = true;
            lastNum = nextNum;
        }
        assertTrue("Calling nextInt 100 times resulted in same number",
                someDifferent);
    }
Example 17
public static int randomLevel(Random rnd) {
        int x = rnd.nextInt();

        int level = 0;

        while ((x & 1) != 0) { // Count sequential 1 bits.
            level++;

            x >>>= 1;
        }

        return level;
    }
Example 18
Random rand = new Random();

    private int[] chooseRandomParams()
    {
        int n = rand.nextInt(10) + 2;
        int[] vi = new int[n];

        vi[0] = rand.nextInt(10) + 2;
        for (int i = 1; i < n; i++)
        {
            vi[i] = vi[i - 1];
            vi[i] += rand.nextInt(10) + 1;
        }
        return vi;
    }
Example 19
final Random rand = new Random();

  private String makeRandomBlockpoolId() {
    return "BP-" + rand.nextInt(Integer.MAX_VALUE) +
           "-" + makeRandomIpAddress() +
           "-" + rand.nextInt(Integer.MAX_VALUE);
  }
Example 20
public void shuffle(Random rand) {
        for (int i = _pos; i-- > 1;) {
            swap(i, rand.nextInt(i));
        }
    }
Example 21
private int getRandom() {
    final Random generator = new Random();
    return generator.nextInt(Integer.MAX_VALUE) == 0 ? generator.nextInt(Integer.MAX_VALUE)
        : generator.nextInt(Integer.MAX_VALUE);
  }
Example 22
public static void addRandomDataToMap() {
        Map<Integer, String> dataMap = new HashMap<>();
        Random r = new Random();
        while (true) {
            dataMap.put(r.nextInt(), String.valueOf(r.nextInt()));
        }
    }
Example 23
private Random rand = new Random();

    public void iterate() throws Exception {

        // always do a new price
        newPrice();

        // 1/10th of the time
        if (rand.nextInt(10) == 0) {
            newOrder();
        }

        // 1/50th of the time
        if (rand.nextInt(50) == 0) {
            newTrade();
        }
    }
Example 24
final private Random _random;

  public int hash(Request request)
  {
    return _random.nextInt();
  }
Example 25
final Random mRandom = new Random();

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xff000000);
        mPaint.setTextSize(mRandom.nextInt(40) + 10);
        mPaint.setColor(0xff000000 + mRandom.nextInt(0x1000000));
        int x = mRandom.nextInt(getWidth()) - (getWidth()/2);
        int y = mRandom.nextInt(getHeight());
        canvas.drawText("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                x, y, mPaint);
    }