List of usage examples for java.util Random nextInt
public int nextInt(int bound)
From source file:Main.java
public static void shuffle(final Object[] array) { final Random r = new Random(); final int limit = array.length; for (int i = 0; i < limit; ++i) { swap(array, i, r.nextInt(limit)); }// w w w . j a va2 s . c o m }
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 w w w . j a v a 2 s .com }
From source file:Main.java
public static List<Integer> randomChoose(int total, int selectedCount) { Random random = new Random(); List<Integer> resultList = new ArrayList<Integer>(); while (resultList.size() < selectedCount) { Integer randomNum = random.nextInt(total) + 1; if (!resultList.contains(randomNum)) { resultList.add(randomNum);// www. ja v a2 s. c o m } } return resultList; }
From source file:Main.java
public static String randomNumericString(int stringLength) { Random generator = new Random(); StringBuilder randomStringBuilder = new StringBuilder(); for (int i = 0; i < stringLength; i++) { randomStringBuilder.append(generator.nextInt(9)); }// w w w. j ava 2 s . co m return randomStringBuilder.toString(); }
From source file:eu.dasish.annotation.backend.Helpers.java
public static UUID generateUUID() { UUID result = UUID.randomUUID(); char[] chars = result.toString().toCharArray(); if (chars[0] >= 'a' && chars[0] <= 'z') { return result; } else {//from w w w . j a va2 s. c o m Random r = new Random(); chars[0] = hexa.charAt(r.nextInt(hexan)); result = UUID.fromString(new String(chars)); return result; } }
From source file:Main.java
/** * get a fixed-length random string, its a mixture of chars in sourceChar * * @param sourceChar//from www . ja v a2 s . c o m * @param length * @return <ul> * <li>if sourceChar is null or empty, return null</li> * <li>if length less than 0, return null</li> * </ul> */ public static String getRandom(char[] sourceChar, int length) { if (sourceChar == null || sourceChar.length == 0 || length < 0) { return null; } StringBuilder str = new StringBuilder(length); Random random = new Random(); for (int i = 0; i < length; i++) { str.append(sourceChar[random.nextInt(sourceChar.length)]); } return str.toString(); }
From source file:Main.java
public static void shuffle(final Object[] array, final long seed) { final Random r = new Random(seed); final int limit = array.length; for (int i = 0; i < limit; ++i) { swap(array, i, r.nextInt(limit)); }//from w ww . ja va2 s. c o m }
From source file:Main.java
/** * Generate a random String//from ww w . j a v a 2 s .c om * * @param values The characters list to use in the randomization * @param len The number of characters in the output String * @return The randomized String */ public static String randomString(char[] values, int len) { Random rnd = new SecureRandom(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { sb.append(values[rnd.nextInt(values.length)]); } return sb.toString(); }
From source file:com.g414.dgen.field.Fields.java
/** returns a random integer in the given range */ public static Field<Integer> getIntField(final String name, final int start, final int stop) { return new Field<Integer>() { private final int range = stop - start; @Override//from w w w. ja v a2 s . c o m public String getName() { return name; } @Override public Integer getValue(String entityId, Map<String, Object> entitySoFar, Random random) { int next = random.nextInt(range); return next + start; } }; }
From source file:Main.java
public static List<String> generateWidth() { List<String> ids = new ArrayList<String>(); Random mRandom = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 50; i++) { sb.delete(0, sb.length());/* w w w . j a v a2 s .c o m*/ int height = mRandom.nextInt(250) + 100; sb.append(height); ids.add(sb.toString()); } return ids; }