Example usage for java.util Random nextInt

List of usage examples for java.util Random nextInt

Introduction

In this page you can find the example usage for java.util Random nextInt.

Prototype

public int nextInt(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:com.gst.common.Utils.java

public static String randomStringGenerator(final String prefix, final int len, final String sourceSetString) {
    final int lengthOfSource = sourceSetString.length();
    final Random rnd = new Random();
    final StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        sb.append((sourceSetString).charAt(rnd.nextInt(lengthOfSource)));
    }//from   ww  w  .j a  v  a  2s  .c  o m
    return (prefix + (sb.toString()));
}

From source file:com.ibm.watson.movieapp.dialog.fvt.config.Utils.java

/**
 * getRandomSample// w  w  w.  j  a  va  2  s . c o m
 * @param num - number of questions
 * @param jsonFile - jsonfile to read
 * @param chat - chat type question
 * @return
 */
public static ArrayList<BaseQuestion> getRandomSample(int num, String jsonFile, boolean chat) {

    ArrayList<BaseQuestion> questions = new ArrayList<BaseQuestion>();
    ArrayList<BaseQuestion> list;
    int index;

    if (chat) {
        list = returnChatList(jsonFile);
    } else {
        list = returnMovieList(jsonFile);
    }

    //if the number of questions requested is greater then the number of questions reassign
    if (num > list.size()) {
        num = list.size();
    }

    //get questions from JSON file
    Random random = new Random();
    while (num > 0) {
        index = random.nextInt(list.size() - 1);
        questions.add(list.get(index));
        num--;
    }

    return questions;
}

From source file:crow.weibo.util.WeiboUtil.java

/**
 * Just a simple implementation of a random number between 123400 and
 * 9999999/*from  w  w w  .j  a  v  a2s  . c  o  m*/
 * 
 * @return
 */
public static String generateNonce() {
    Random random = new Random();
    return String.valueOf(random.nextInt(9876599) + 123400);
}

From source file:audio.cords.old.RegressionDemo.java

private static XYSeriesCollection getTestData() {
    Random rg = new Random();
    XYSeriesCollection data = new XYSeriesCollection();
    for (int i = 1; i <= 3; i++) {
        XYSeries series = new XYSeries("Series " + i);
        double a = rg.nextDouble() - .5;
        int b = rg.nextInt(20) - 10;
        for (int j = 1; j <= 20; j++) {
            double x = j + (rg.nextDouble() - .5);
            double y = a * j + b + (rg.nextDouble() - .5) * 2;
            series.add(x, y);/*from ww w.j  av a2 s .c  o  m*/
        }
        data.addSeries(series);
    }
    return data;
}

From source file:biz.c24.io.spring.integration.samples.fpml.FpmlGenerator.java

private static TradeConfirmed randomizeFpML(TradeConfirmed tradeConfirmed) throws CloneNotSupportedException {
    ISO8601DateTime creationDate = new ISO8601DateTime(new Date());
    ISO8601Date tradeDate = new ISO8601Date(new GregorianCalendar(2011, Calendar.JULY, 5).getTime());
    ISO8601Date expiryDate = new ISO8601Date(new GregorianCalendar(2012, Calendar.JANUARY, 5).getTime());
    ISO8601Date settlementDate = new ISO8601Date(new GregorianCalendar(2011, Calendar.JULY, 7).getTime());
    ISO8601Date valueDate = new ISO8601Date(new GregorianCalendar(2012, Calendar.JANUARY, 9).getTime());

    Random random = new Random();

    BigDecimal putAmount = new BigDecimal((1 + random.nextInt(999)) * 100000.);
    BigDecimal callAmount = new BigDecimal((1 + random.nextInt(999)) * 100000.);
    BigDecimal fxRate = callAmount.divide(putAmount, 8, RoundingMode.HALF_EVEN).setScale(5,
            BigDecimal.ROUND_HALF_UP);
    BigDecimal premiumValue = new BigDecimal(0.001).setScale(5, BigDecimal.ROUND_HALF_UP);
    BigDecimal premiumAmount = callAmount.multiply(premiumValue).setScale(2, BigDecimal.ROUND_HALF_UP);

    String sendTo = "SendTo" + random.nextInt(999999);
    String sentBy = "SentBy" + random.nextInt(999999);
    String msgId = "Msg" + random.nextInt(999999);
    String conversationId = "Conv" + random.nextInt(999999);
    String tradeId1 = "ID1:" + random.nextInt(999999);
    String tradeId2 = "ID2:" + random.nextInt(999999);

    // Set the header
    NotificationMessageHeader header = tradeConfirmed.getHeader();
    header.getMessageHeadermodel().setCreationTimestamp(creationDate);
    header.getConversationId().setValue(conversationId);
    header.getMessageId().setValue(msgId);
    header.getMessageHeadermodel().getSentBy().setValue(sentBy);
    header.getMessageHeadermodel().getSendTo()[0].setValue(sendTo);

    // Set the TradeHeader
    TradeHeader tradeHeader = tradeConfirmed.getTrade().getTradeHeader();
    tradeHeader.getPartyTradeIdentifier()[0].getTradeIdentifierSG1()[0].getTradeId().setValue(tradeId1);
    tradeHeader.getPartyTradeIdentifier()[1].getTradeIdentifierSG1()[0].getTradeId().setValue(tradeId2);
    tradeHeader.getTradeDate().setValue(tradeDate);

    FxOptionLeg fxOptionLeg = (FxOptionLeg) tradeConfirmed.getTrade().getProduct();
    fxOptionLeg.getExpiryDateTime().setExpiryDate(expiryDate);

    FxOptionPremium optionPremium = fxOptionLeg.getFxOptionPremium()[0];
    optionPremium.getPremiumAmount().setAmount(premiumAmount);
    optionPremium.setPremiumSettlementDate(settlementDate);
    SettlementInstruction settlementInstruction = optionPremium.getSettlementInformation()
            .getSettlementInstruction();
    settlementInstruction.getCorrespondentInformation().getRoutingIdentificationmodel().getRoutingIds()
            .getRoutingId()[0].setValue(sendTo);
    settlementInstruction.getBeneficiary().getRoutingIdentificationmodel().getRoutingIds().getRoutingId()[0]
            .setValue(sentBy);/* www .  ja va 2  s .  com*/
    optionPremium.getPremiumQuote().setPremiumValue(premiumValue);
    fxOptionLeg.setValueDate(valueDate);

    fxOptionLeg.getPutCurrencyAmount().getCurrency().setValue("AUD");
    fxOptionLeg.getPutCurrencyAmount().setAmount(putAmount);

    fxOptionLeg.getPutCurrencyAmount().getCurrency().setValue("USD");
    fxOptionLeg.getCallCurrencyAmount().setAmount(callAmount);

    fxOptionLeg.getFxStrikePrice().setRate(fxRate);

    fxOptionLeg.getQuotedAs().getOptionOnCurrency().setValue("AUD");
    fxOptionLeg.getQuotedAs().getFaceOnCurrency().setValue("USD");
    fxOptionLeg.getQuotedAs().getQuotedTenor().setPeriod("M");
    fxOptionLeg.getQuotedAs().getQuotedTenor().setPeriodMultiplier(new BigInteger("6"));

    tradeConfirmed.getParty()[0].getPartyId()[0].setValue(sendTo);
    tradeConfirmed.getParty()[1].getPartyId()[0].setValue(sentBy);

    return (TradeConfirmed) tradeConfirmed.cloneDeep();
}

From source file:com.inclouds.hbase.utils.RegionServerPoker.java

/**
  * Selects random region/*from w  w w  .  j a  v  a  2  s .com*/
  * @param set
  * @return
  */
private static HRegionInfo select(Set<HRegionInfo> set) {
    Random r = new Random();
    int i = r.nextInt(set.size());
    return (HRegionInfo) set.toArray()[i];

}

From source file:Main.java

public static <T> ArrayList<T> rand(ArrayList<T> population, int nSamplesNeeded) {
    Random r = new Random();
    ArrayList<T> ret = new ArrayList<T>();

    if (nSamplesNeeded > population.size() / 2) {
        ArrayList<T> original = new ArrayList<T>();
        original = population;/*from   w w w .j ava2 s  .  c o  m*/

        while (nSamplesNeeded > 0) {
            int rand = r.nextInt(original.size());
            if (rand < nSamplesNeeded) {
                ret.add(original.get(rand));
                original.remove(rand);
                nSamplesNeeded--;
            }
        }
        original.clear();
    } else
        ret = shuffle(population, nSamplesNeeded);

    return ret;
}

From source file:com.pasi.pdfbox.Main.java

private static PatientBloodPressureReport createPatientBloodPressureReport() {
    PatientBloodPressureReport report = new PatientBloodPressureReport();
    report.setPatientName("Bean Zeng");
    report.setDuration(3);//from w w  w. j a v  a2  s. co  m
    report.setReportDate(new Date());
    report.setComments("N/A");
    report.setDesiredSystolic(80);
    report.setDesiredDiastolic(120);
    List<BloodPressureRecord> records = new ArrayList<BloodPressureRecord>();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        BloodPressureRecord record = new BloodPressureRecord();
        record.setDate(new Date(System.currentTimeMillis() - (10 - i) * 24 * 60 * 60 * 1000));
        record.setSystolic(80 + random.nextInt(10));
        record.setDiastolic(120 + random.nextInt(20));
        record.setPulse(75 + random.nextInt(10));
        record.setSource("Source #" + i);
        records.add(record);
    }
    report.setBloodPressureRecords(records);
    return report;
}

From source file:com.jkoolcloud.tnt4j.streams.custom.kafka.interceptors.InterceptorsTest.java

private static void produce(Producer<String, String> producer, String topic, int eventCount) {
    Random rnd = new Random();

    for (int ei = 0; ei < eventCount; ei++) {
        long runtime = System.currentTimeMillis();
        String ip = "192.168.2." + rnd.nextInt(255); // NON-NLS
        String msg = runtime + ",www.example.com," + ip + "," + (ei + 1)
                + eventsPayload[ei / eventsPayload.length];// NON-NLS
        ProducerRecord<String, String> data = new ProducerRecord<>(topic, ip, msg);
        producer.send(data);//from w  ww  .  j a  v a 2  s.  co m
        LOGGER.log(OpLevel.INFO, "Producing Kafka message: seqNumber={2} msg={1}", producer.hashCode(), // NON-NLS
                data, ei);
        if (ei % 5 == 0) {
            try {
                Thread.sleep((long) (1000 + 2000 * Math.random()));
            } catch (InterruptedException exc) {
            }
        }
    }

    producer.close();
}

From source file:ezbake.locksmith.service.EzLocksmithHandlerTest.java

@BeforeClass
public static void init() throws Exception {
    BaseTest.init();/*from w ww  .  j  a v  a 2s.c o m*/

    Random portChooser = new Random();
    int port = portChooser.nextInt((23999 - 22999) + 1) + 22999;
    serverPool = new ThriftServerPool(ezConfiguration, port);
    serverPool.startCommonService(new EzLocksmithHandler(), SERVICE_NAME, "gibberish1234");
    clientPool = new ThriftClientPool(ezConfiguration);

    mongo = new MongoDBService(ezConfiguration);
    EzBakeApplicationConfigurationHelper appConfig = new EzBakeApplicationConfigurationHelper(ezConfiguration);
    rsaTable = String.format("lock_smith_%s_%s_%s", appConfig.getApplicationName(), appConfig.getServiceName(),
            "rsa_keys");
    aesTable = String.format("lock_smith_%s_%s_%s", appConfig.getApplicationName(), appConfig.getServiceName(),
            "aes_keys");
}