Example usage for java.lang Math random

List of usage examples for java.lang Math random

Introduction

In this page you can find the example usage for java.lang Math random.

Prototype

public static double random() 

Source Link

Document

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 .

Usage

From source file:com.splout.db.examples.PageCountsDataGenerator.java

public void generate(File namesFile, int days, File outFolder) throws IOException {
    if (outFolder.exists()) {
        FileUtils.deleteDirectory(outFolder);
    }//from  w  w  w . jav a 2s  .  c o m
    outFolder.mkdirs();

    if (!namesFile.exists()) {
        throw new IllegalArgumentException("Provided names file doesn't exist (" + namesFile + ")");
    }

    List<String> names = Files.readLines(namesFile, Charset.forName("UTF-8"));

    DateTime today = new DateTime();
    DateTime someDaysBefore = today.minusDays(days);

    someDaysBefore = someDaysBefore.withMinuteOfHour(0);
    someDaysBefore = someDaysBefore.withSecondOfMinute(0);

    while (someDaysBefore.isBefore(today)) {
        for (int hour = 0; hour < 24; hour++) {

            someDaysBefore = someDaysBefore.withHourOfDay(hour);

            File currentFile = new File(outFolder, "pagecounts-" + format.print(someDaysBefore));
            BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));

            for (String name : names) {
                int pageviews = (int) (Math.random() * 10000) + 1;
                writer.write("en " + name + " " + pageviews + " 0" + "\n");
            }

            writer.close();
        }
        someDaysBefore = someDaysBefore.plusDays(1);
    }
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionServiceIT.java

/**
 * Create applications and infrastructures to associate the
 * tasks with.// ww  w  .  java  2  s. co m
 */
@Before
public final void prepareApplication() {
    infras = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestData.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
        Response rs = target("/v1.0/infrastructures").request(Constants.INDIGOMIMETYPE)
                .post(Entity.entity(TestData.createInfrastructure(), Constants.INDIGOMIMETYPE));
        infras.add(rs.readEntity(Infrastructure.class).getId());
    }

    apps = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestData.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
        Application app = TestData.createApplication();
        app.setInfrastructureIds(infras);
        Response rs = target("/v1.0/applications").request(Constants.INDIGOMIMETYPE)
                .post(Entity.entity(app, Constants.INDIGOMIMETYPE));
        apps.add(rs.readEntity(Application.class).getId());
    }
}

From source file:com.seleniumtests.browserfactory.mobile.LocalAppiumLauncher.java

public LocalAppiumLauncher(String logDirectory) {

    appiumPort = 4723 + Math.round(Math.random() * 1000);
    if (logDirectory != null) {
        new File(logDirectory).mkdirs();
        if (new File(logDirectory).isDirectory()) {
            logFile = Paths.get(logDirectory, String.format("appium-%d.log", appiumPort)).toString();
        }//from  w w  w  .  j  a  v a2s. com
    }

    checkInstallation();
    generateOptions();

}

From source file:com.ciphertool.sentencebuilder.dao.BasicWordMapDao.java

@Override
public Word findRandomWordByPartOfSpeech(PartOfSpeechType pos) {
    ArrayList<Word> wordList = partOfSpeechWordMap.get(pos);

    int randomIndex = (int) (Math.random() * wordList.size());

    return wordList.get(randomIndex);
}

From source file:com.sm.store.TestStoreCases.java

public long genLong(long seed) {
    if (seed < Integer.MAX_VALUE)
        return random.nextInt((int) seed);
    else {/* w w  w .j  a v a2  s  . co m*/
        return (long) Math.random() * seed;
    }
}

From source file:com.pureinfo.tgirls.servlet.GetScriptServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String scriptType = request.getParameter("scriptType");
    if (StringUtils.isEmpty(scriptType)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;//from w w  w .j a v  a  2 s . c o m
    }

    String location = null;
    String userId = null;
    String photoId = null;
    ScriptType st = null;//

    try {
        st = ScriptType.getScriptType(Integer.parseInt(scriptType));
    } catch (Exception e) {
    }

    switch (st) {
    case UserInfo:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserInfoScriptLocation(userId);
        break;
    case UserUpload:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserUploadScriptLocation(userId);
        break;
    case UserBuy:
        userId = request.getParameter("utid");
        location = ScriptManager.mapUserBuyScriptLocation(userId);
        break;
    case Pic:
        photoId = request.getParameter("picId");
        location = ScriptManager.mapPhotoScriptLocation(photoId);
        break;

    default:
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (StringUtils.isEmpty(location)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    location += "?version=" + Math.random();
    logger.debug("return script:" + location);

    response.setHeader("Location", location);
    response.setHeader("Content-Encoding", "gzip");
    response.setStatus(HttpServletResponse.SC_SEE_OTHER);
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", -1);
    response.setDateHeader("max-age", 0);

    return;

}

From source file:net.solarnetwork.node.power.foobar.FoobarDatumDataSource.java

@Override
public PowerDatum readCurrentDatum() {
    // our inverter is a 1kW system, let's produce a random value between 0-1000
    int watts = (int) Math.round(Math.random() * 1000.0);

    // we'll increment our Wh reading by a random amount between 0-15, with
    // the assumption we will read samples once per minute
    long wattHours = wattHourReading.addAndGet(Math.round(Math.random() * 15.0));

    PowerDatum datum = new PowerDatum();
    datum.setCreated(new Date());
    datum.setWatts(watts);// ww  w .j  a  v  a 2  s  .c  o m
    datum.setWattHourReading(wattHours);
    datum.setSourceId(sourceId);
    postDatumCapturedEvent(datum, PowerDatum.class);
    return datum;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskServiceIT.java

/**
 * Create applications and infrastructures to associate the
 * tasks with./*  w w  w . j a v  a 2 s  . com*/
 */
@Before
public final void prepareApplication() {
    infras = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestData.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
        Entity<Infrastructure> infraEntity = Entity.entity(TestData.createInfrastructure(),
                Constants.INDIGOMIMETYPE);
        Response rs = target("/v1.0/infrastructures").request(Constants.INDIGOMIMETYPE).post(infraEntity);
        infras.add(rs.readEntity(Infrastructure.class).getId());
    }

    apps = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestData.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
        Application app = TestData.createApplication();
        app.setInfrastructureIds(infras);
        Entity<Application> appEntity = Entity.entity(app, Constants.INDIGOMIMETYPE);
        Response rs = target("/v1.0/applications").request(Constants.INDIGOMIMETYPE).post(appEntity);
        apps.add(rs.readEntity(Application.class).getId());
    }
}

From source file:org.jfree.chart.demo.XYBlockChartDemo2.java

private static XYZDataset createDataset() {
    double ad[] = new double[2400];
    double ad1[] = new double[2400];
    double ad2[] = new double[2400];
    Object obj = new Day();
    for (int i = 0; i < 100; i++) {
        double d = 1.0D;
        for (int j = 0; j < 24; j++) {
            if (Math.random() < 0.10000000000000001D)
                d = Math.random() * 4D;
            ad[i * 24 + j] = ((RegularTimePeriod) (obj)).getFirstMillisecond();
            ad1[i * 24 + j] = j;//from   w w w  .  j av a 2s. c o  m
            ad2[i * 24 + j] = d;
        }

        obj = ((RegularTimePeriod) (obj)).next();
    }

    DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset();
    defaultxyzdataset.addSeries("Series 1", new double[][] { ad, ad1, ad2 });
    return defaultxyzdataset;
}

From source file:com.c123.billbuddy.client.UserFeeder.java

@PostConstruct
public void init() throws Exception {
    log.info("Starting User Feeder");

    Integer userAccountId = 1;//from  w  w  w . ja  v a  2 s  . co  m

    for (String u : userList) {

        User foundUser = gigaSpace.readById(User.class, userAccountId);

        if (foundUser == null) {
            User user = new User();
            user.setName(u);

            Double balance = (Double.valueOf(Math.random() * 10000));
            user.setBalance(Math.round(balance * 100.0) / 100.0);

            Double creditLimit = Math.random() * 10000;
            creditLimit = creditLimit - (creditLimit % 1000);
            creditLimit = Math.round(creditLimit * 100.0) / 100.0;

            user.setCreditLimit(Double.valueOf(-(creditLimit)));
            user.setStatus(AccountStatus.ACTIVE);
            user.setUserAccountId(userAccountId);

            Address tempAddress = new Address();
            tempAddress.setCountry(CountryNames.values()[new Random().nextInt(CountryNames.values().length)]);
            tempAddress.setCity("123Completed.com");
            tempAddress.setState("GIGASPACES");
            tempAddress.setStreet("Here and There");
            tempAddress.setZipCode(new Random().nextInt());

            user.setAddress(tempAddress);

            // Writing the new user to the space
            gigaSpace.write(user);
            log.info(String.format("Added User object with name '%s'", user.getName()));
        }
        userAccountId++;
    }

    log.info("Stopping User Feeder");
}