List of usage examples for java.util Random Random
public Random()
From source file:Main.java
/** * Scramble oder of elements in a list.//from w w w . j a v a2s . c om * * @param list */ public static <E> void scrambleList(List<E> list) { if (isNotEmpty(list)) { Random generator = new Random(); for (int i = 0; i < list.size(); i++) { int random = generator.nextInt(list.size()); if (random != i) { //Util of vs.(swap) E objTemp = list.get(i); list.set(i, list.get(random)); list.set(random, objTemp); } } } }
From source file:Main.java
public static void shuffle(final int[] array) { final Random r = new Random(); final int limit = array.length; for (int i = 0; i < limit; ++i) { swap(array, i, r.nextInt(limit)); }/*from www . j a v a 2s. c o m*/ }
From source file:Test.java
private static void startUpdatingThread(final List<String> list) { updatingThread = new Thread(new Runnable() { long counter = 0; public void run() { while (!Thread.interrupted()) { int size = list.size(); Random random = new Random(); if (random.nextBoolean()) { if (size > 1) { list.remove(random.nextInt(size - 1)); }//from www . jav a 2s. com } else { if (size < 20) { list.add("Random string " + counter); } } counter++; } } }); updatingThread.start(); }
From source file:Main.java
public static byte[][] splitUnicodeMessage(byte[] aMessage, Integer maximumMultipartMessageSegmentSize) { final byte UDHIE_HEADER_LENGTH = 0x05; final byte UDHIE_IDENTIFIER_SAR = 0x00; final byte UDHIE_SAR_LENGTH = 0x03; // determine how many messages have to be sent int numberOfSegments = aMessage.length / maximumMultipartMessageSegmentSize; int messageLength = aMessage.length; if (numberOfSegments > 255) { numberOfSegments = 255;/* w ww . j a v a 2 s . c om*/ messageLength = numberOfSegments * maximumMultipartMessageSegmentSize; } if ((messageLength % maximumMultipartMessageSegmentSize) > 0) { numberOfSegments++; } // prepare array for all of the msg segments byte[][] segments = new byte[numberOfSegments][]; int lengthOfData; // generate new reference number byte[] referenceNumber = new byte[1]; new Random().nextBytes(referenceNumber); // split the message adding required headers for (int i = 0; i < numberOfSegments; i++) { if (numberOfSegments - i == 1) { lengthOfData = messageLength - i * maximumMultipartMessageSegmentSize; } else { lengthOfData = maximumMultipartMessageSegmentSize; } // new array to store the header segments[i] = new byte[6 + lengthOfData]; // UDH header // doesn't include itself, its header length segments[i][0] = UDHIE_HEADER_LENGTH; // SAR identifier segments[i][1] = UDHIE_IDENTIFIER_SAR; // SAR length segments[i][2] = UDHIE_SAR_LENGTH; // reference number (same for all messages) segments[i][3] = referenceNumber[0]; // total number of segments segments[i][4] = (byte) numberOfSegments; // segment number segments[i][5] = (byte) (i + 1); // copy the data into the array System.arraycopy(aMessage, (i * maximumMultipartMessageSegmentSize), segments[i], 6, lengthOfData); } return segments; }
From source file:lifetime.backend.util.TestHelper.java
/** * Returns a new random byte array.//from w w w .j a v a 2s. c o m * * @return A byte array. */ public static byte[] getRandomByteArray() { byte[] result = new byte[1024]; new Random().nextBytes(result); return result; }
From source file:Main.java
public static int getRandom(int min, int max) { if (min > max) { return 0; }/* w w w .j ava 2 s . co m*/ if (min == max) { return min; } return min + new Random().nextInt(max - min); }
From source file:fakedatamaker.util.RandomFileUtil.java
/** * //from w w w. jav a 2 s. c o m * @param filePath * @param encoding * @return */ public static String getRandomFileLine(String filePath, String encoding) { String result = null; try { File file = new File(filePath); List<String> cityLines = FileUtils.readLines(file, encoding); int numberOfCities = cityLines.size(); Random r = new Random(); int cityLineNum = r.nextInt(numberOfCities); result = cityLines.get(cityLineNum); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
From source file:Main.java
/** * Given a list, returns a random element from that list. * * @param list The list to return a random element from * * @param <T> The element type of the list * * @return A random element from the list *///from w w w. j av a2 s . co m public static <T> T getRandomElement(List<T> list) { return list.get(new Random().nextInt(list.size())); }
From source file:Main.java
/** Returns temp dir, containing String arg in its name; * does not create the directory. */ public static File getTempDir(String desc) { String tempDir = System.getProperty("java.io.tmpdir"); if (tempDir == null) throw new RuntimeException("java.io.tmpdir undefined, cannot run test"); return new File(tempDir, desc + "." + new Random().nextLong()); }
From source file:com.saax.gestorweb.util.DAOAleatorio.java
public static Departamento getDepartamentoAleatorio(EntityManager em, Empresa e) { List<Departamento> departamentos = em.createNamedQuery("Departamento.findByEmpresa") .setParameter("empresa", e).getResultList(); return departamentos.get(new Random().nextInt(departamentos.size() - 1)); }