List of usage examples for java.util Random nextInt
public int nextInt(int bound)
From source file:setiquest.renderer.Utils.java
/** * Get a random file from a directory.//from w w w .j av a 2 s.c o m * @param dir the directory to search in. * @return a file name full path. */ public static String getRandomFile(String dir) { File directory = new File(dir); int count = 0; String randomFileName = ""; if (directory.exists()) { File[] listFiles = directory.listFiles(); int numFiles = listFiles.length; Random rand = new Random(System.currentTimeMillis()); int index = rand.nextInt(numFiles - 1); randomFileName = listFiles[index].getAbsolutePath(); } return randomFileName; }
From source file:com.datumbox.framework.common.utilities.PHPMethods.java
/** * Shuffles the values of any array in place. * /*from w w w .ja va 2 s .c o m*/ * @param <T> * @param array */ public static <T> void shuffle(T[] array) { //Implementing Fisher-Yates shuffle T tmp; Random rnd = RandomGenerator.getThreadLocalRandom(); for (int i = array.length - 1; i > 0; --i) { int index = rnd.nextInt(i + 1); tmp = array[index]; array[index] = array[i]; array[i] = tmp; } }
From source file:demo.enumj.EnumeratorDemo.java
private static void demoChoiceOf(String pre) { final Random rnd = new Random(); final Enumerator<Integer> en = Enumerator.choiceOf(() -> rnd.nextInt(2), _123.iterator(), Enumerator.on(4, 5, 6, 7, 8)); printLn(en, "Randomly chosen elements:", pre); }
From source file:com.impetus.ankush.common.utils.PasswordUtil.java
/** * Gets the random password./*from w w w . j a v a 2s.co m*/ * * @param length the length * @param smallChars the small chars * @param capsChars the caps chars * @param digits the digits * @param specialChars the special chars * @return the random password */ private static String getRandomPassword(int length, char[] smallChars, char[] capsChars, char[] digits, char[] specialChars) { char[][] charsArrArr = { smallChars, capsChars, digits, specialChars }; String password = null; final int charSetCategories = 4; StringBuilder passwordGenBuff = new StringBuilder(); Random r = new Random(); int arrIdx[] = new int[charSetCategories]; for (int pos = 0; pos < charSetCategories; ++pos) { arrIdx[pos] = 0; } while (passwordGenBuff.length() < length) { int setIdx = r.nextInt(charSetCategories); char[] chArr = charsArrArr[setIdx]; if ((chArr == null) || (chArr.length == 0)) { continue; } else { int generatedPasswordLength = passwordGenBuff.length(); int untouchedSetCount = 0; if (generatedPasswordLength > 0) { for (int idx = 0; idx < charSetCategories; idx++) { char[] currArr = charsArrArr[idx]; if ((currArr != null) && (currArr.length > 0)) { if (arrIdx[idx] == 0) { ++untouchedSetCount; } } } if ((untouchedSetCount >= (length - generatedPasswordLength)) && (arrIdx[setIdx] > 0)) { continue; } } passwordGenBuff.append(RandomStringUtils.random(1, chArr)); ++arrIdx[setIdx]; } } password = passwordGenBuff.toString(); if (r.nextInt() % 2 == 0) { password = StringUtils.reverse(password); } return passwordGenBuff.toString(); }
From source file:hivemall.mix.server.MixServerTest.java
private static void invokeClient(String groupId, int serverPort) throws InterruptedException { PredictionModel model = new DenseModel(16777216, false); model.configureClock();/*from www . j a v a 2 s . co m*/ MixClient client = null; try { client = new MixClient(MixEventName.average, groupId, "localhost:" + serverPort, false, 2, model); model.configureMix(client, false); final Random rand = new Random(43); for (int i = 0; i < 100000; i++) { Integer feature = Integer.valueOf(rand.nextInt(100)); float weight = (float) rand.nextGaussian(); model.set(feature, new WeightValue(weight)); } waitForMixed(model, 48000L, 10000L); long numMixed = model.getNumMixed(); Assert.assertTrue("number of mix events: " + numMixed, numMixed > 0); } finally { IOUtils.closeQuietly(client); } }
From source file:Main.java
public static void shuffleSubArray(int[] array, int subArrayLength, Random rand) { if (subArrayLength == 0) return;//from w w w.ja va 2s . c o m int subArrays = array.length / subArrayLength; for (int subArraysNum = 0; subArraysNum < subArrays; subArraysNum++) { int subArrayEndIndex = (subArraysNum + 1) * subArrayLength; int subArrayStartIndex = subArrayEndIndex - subArrayLength; for (int i = subArrayEndIndex; i > subArrayStartIndex + 1; i--) { int b = rand.nextInt(subArrayEndIndex - subArrayStartIndex) + subArrayStartIndex; if (b != i - 1) { int e = array[i - 1]; array[i - 1] = array[b]; array[b] = e; } } } }
From source file:com.wabacus.util.FileLockTools.java
public static Object lock(String lockfile, int waitsec, int maxtimes) { Object lockresource = lock(lockfile); Random random = new Random(); int times = 0; while (lockresource == null) { if (times++ >= maxtimes) { log.debug("??" + lockfile + "?" + maxtimes + "?"); break; }/*from w w w.j a v a 2s . c o m*/ try { Thread.sleep(random.nextInt(waitsec) * 1000); } catch (Exception e) { e.printStackTrace(); } lockresource = lock(lockfile); } return lockresource; }
From source file:hivemall.mix.server.MixServerTest.java
private static void invokeClient01(String groupId, int serverPort, boolean denseModel, boolean cancelMix) throws InterruptedException { PredictionModel model = denseModel ? new DenseModel(100, false) : new SparseModel(100, false); model.configureClock();/* w w w . j av a 2s . c o m*/ MixClient client = null; try { client = new MixClient(MixEventName.average, groupId, "localhost:" + serverPort, false, 3, model); model.configureMix(client, cancelMix); final Random rand = new Random(43); for (int i = 0; i < 1000000; i++) { Integer feature = Integer.valueOf(rand.nextInt(100)); float weight = rand.nextFloat() >= 0.5f ? 1.f : 0.f; model.set(feature, new WeightValue(weight)); } waitForMixed(model, 100000L, 10000L); long numMixed = model.getNumMixed(); Assert.assertTrue("number of mix events: " + numMixed, numMixed > 0); for (int i = 0; i < 100; i++) { float w = model.getWeight(i); Assert.assertEquals(0.5f, w, 0.1f); } } finally { IOUtils.closeQuietly(client); } }
From source file:com.thejustdo.util.Utils.java
/** * Builds a random code for a new Dream Box. * * @param prefix What must be putted before the real code. * @param length How long must be the new number * @return The built code.//from w ww .j a va 2s . co m */ public static String buildRandomCode(String prefix, int length) { // 1. Gatter the prefix size. int prefixL = prefix.length(); int n = length - prefixL; // 2. Build the n digit code. String code = ""; Random random = new Random(); do { code += random.nextInt(10); } while (code.length() < n); return prefix + code; }
From source file:com.phoenixst.plexus.examples.RandomGraphFactory.java
/** * Creates a random graph according to the Barabasi-Albert model. * * <P>Start with <code>numInitialNodes</code> nodes. At each * step, add a new node which is connected to * <code>numEdges</code> existing nodes, with preference given to * nodes that are more highly connected. *///from w ww . ja v a2 s . c o m public static Graph createBarabasiAlbert(int numInitialNodes, int numFinalNodes, int numEdges) { if (numEdges > numInitialNodes) { throw new IllegalArgumentException("Number of edges cannot be more than the number of initial nodes."); } Graph graph = new DefaultGraph(new EmptyGraph(numInitialNodes + 1)); Random random = new Random(); // Start out with numEdges randomly connected from the last // node to other nodes. Object tail = new Integer(numInitialNodes); for (int k = 0; k < numEdges; k++) { while (true) { Object head = new Integer(random.nextInt(numInitialNodes)); Predicate traverserPred = TraverserPredicateFactory.createEqualsNode(head, GraphUtils.ANY_DIRECTION_MASK); if (graph.getIncidentEdge(tail, traverserPred) == null) { graph.addEdge(null, tail, head, true); break; } } } // Now, do the rest of the work int totalDegree = numEdges * 2; for (int i = numInitialNodes + 1; i < numFinalNodes; i++, totalDegree += numEdges * 2) { tail = new Integer(i); graph.addNode(tail); for (int k = 0; k < numEdges; k++) { while (true) { int randomDegreeSum = random.nextInt(totalDegree); Object head = new Integer(0); int degreeSum = 0; for (int headIndex = 0; degreeSum < randomDegreeSum; headIndex++) { head = new Integer(headIndex); degreeSum += graph.degree(head); } Predicate traverserPred = TraverserPredicateFactory.createEqualsNode(head, GraphUtils.ANY_DIRECTION_MASK); if (graph.getIncidentEdge(tail, traverserPred) == null) { graph.addEdge(null, tail, head, true); break; } } } } return graph; }