List of usage examples for java.util Random nextInt
public int nextInt(int bound)
From source file:com.meltmedia.dropwizard.etcd.json.EtcdWatchServiceIT.java
public static Thread startNoiseThread(EtcdDirectoryDao<NoiseDocument> dao, int count) { Random random = new Random(); Thread events = new Thread(() -> { for (int i = 0; i < count; i++) { switch (random.nextInt(4)) { case 0: case 1: case 2: dao.put("noise_" + String.valueOf(i), new NoiseDocument().withNoise(String.valueOf(i))); break; case 3: dao.putDir("/noise_" + String.valueOf(i)); break; }//from ww w. ja v a2 s. c o m } }); events.start(); return events; }
From source file:com.hmiard.blackwater.projects.Builder.java
/** * Appending a new server to a project.//ww w . j av a2 s .c o m * * @param projectRoot String * @param serverName String * @param consoleListener ConsoleEmulator * @param needsDb Boolean */ public static Boolean appendServer(String projectRoot, String serverName, ConsoleEmulator consoleListener, Boolean needsDb) { try { serverName = serverName.substring(0, 1).toUpperCase() + serverName.substring(1).toLowerCase(); String shortServerName = serverName; serverName += "Server"; String src = projectRoot + "\\src\\" + serverName; File checker = new File(projectRoot + "\\src\\" + serverName); File composerJSON = new File(projectRoot + "\\composer.json"); if (checker.exists() && checker.isDirectory()) { consoleListener.push("This server already exists ! Operation aborted."); return false; } if (!composerJSON.exists()) { consoleListener.push("File composer.json is missing ! Operation aborted."); return false; } if (needsDb) copyFolder(new File("resources/packages/DefaultApp"), new File(src)); else copyFolder(new File("resources/packages/NoDbApp"), new File(src)); FileOutputStream writer; File core = new File(src + "\\BlackwaterDefaultApp.php"); File qf = new File(src + "\\DefaultAppQueryFactory.php"); File bootstrap = new File(src + "\\bin\\init.php"); String coreContent = readFile(core.getAbsolutePath()); coreContent = coreContent.replace("BlackwaterDefaultApp", serverName); File newCore = new File(src + "\\" + serverName + ".php"); if (newCore.createNewFile() && core.delete()) { writer = new FileOutputStream(newCore); writer.write(coreContent.getBytes()); writer.flush(); writer.close(); } if (needsDb) { String qfContent = readFile(qf.getAbsolutePath()); qfContent = qfContent.replace("BlackwaterDefaultApp", serverName); qfContent = qfContent.replace("DefaultApp", shortServerName); File newQf = new File(src + "\\" + shortServerName + "QueryFactory.php"); if (newQf.createNewFile() && qf.delete()) { writer = new FileOutputStream(newQf); writer.write(qfContent.getBytes()); writer.flush(); writer.close(); } } String bootsrapContent = readFile(bootstrap.getAbsolutePath()); Random r = new Random(); bootsrapContent = bootsrapContent.replace("Default", shortServerName); bootsrapContent = bootsrapContent.replace("8080", String.valueOf(r.nextInt(2000) + 7000)); writer = new FileOutputStream(bootstrap); writer.write(bootsrapContent.getBytes()); writer.flush(); writer.close(); JSONObject composer = new JSONObject(readFile(composerJSON.getAbsolutePath())); JSONObject autoload = composer.getJSONObject("autoload"); JSONObject psr0 = autoload.getJSONObject("psr-0"); psr0.put(serverName, "src"); BufferedWriter cw = new BufferedWriter(new FileWriter(composerJSON.getAbsoluteFile())); String content = composer.toString(4).replaceAll("\\\\", ""); cw.write(content); cw.close(); consoleListener.push(serverName + " created !\n"); } catch (JSONException | IOException e) { e.printStackTrace(); } return true; }
From source file:com.dungnv.vfw5.base.utils.StringUtils.java
public static String generateRandomString(int length) { Random rngRandom = new Random(); char[] text = new char[length]; int lengRnd = RANDOM_STRING.length(); for (int i = 0; i < length; i++) { text[i] = RANDOM_STRING.charAt(rngRandom.nextInt(lengRnd)); }// w ww. ja v a2 s. c o m return new String(text); }