Example usage for java.util Random nextInt

List of usage examples for java.util Random nextInt

Introduction

In this page you can find the example usage for java.util Random nextInt.

Prototype

public int nextInt(int bound) 

Source Link

Document

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Usage

From source file:org.eclipse.swt.snippets.Snippet333.java

protected static void setBackground(Display display, Control control) {
    Random randomGenerator = new Random();
    int nextColor = randomGenerator.nextInt(100) + randomGenerator.nextInt(100);
    control.setBackground(display.getSystemColor(colors[nextColor % colors.length]));
}

From source file:net.dovemq.transport.link.LinkTestUtils.java

public static CAMQPMessagePayload createMessagePayload(Random randomGenerator) {
    int sectionSize = 256 * (randomGenerator.nextInt(10) + 1);
    String str = RandomStringUtils.randomAlphanumeric(sectionSize);
    return new CAMQPMessagePayload(str.getBytes());
}

From source file:Main.java

public static String getRandomString(int len) {
    String returnStr = "";
    char[] ch = new char[len];
    Random rd = new Random();
    for (int i = 0; i < len; i++) {
        ch[i] = (char) (rd.nextInt(9) + 97);
    }/*  w  w  w. ja va 2s . c om*/
    returnStr = new String(ch);
    return returnStr;
}

From source file:Main.java

public static int[] randomIntArray(int length) {
    int[] ran = new int[length];
    Random random = new Random();
    for (int n = 0; n < ran.length; n++) {
        ran[n] = random.nextInt(100);
    }//from   w w w.j a v  a2  s.com
    return ran;
}

From source file:fakedatamaker.util.RandomFileUtil.java

/**
 * /* w w w.  j av  a  2 s.c om*/
 * @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

/**
 * Returns a valid IP v4 address.//  ww  w.j a  va  2  s . co  m
 *
 * @return [0-255].[0-255].[0-255].[0-255]
 */
public static String generateRandomIp() {
    try {
        Random r = new Random();
        String ip = r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
        if (isValidIp(ip)) {
            return ip;
        } else {
            return generateRandomIp();
        }
    } catch (Exception e) {
    }
    return "127.0.0.1";
}

From source file:Main.java

public static Calendar getRandomApodDate() {
    Calendar newCalendar = Calendar.getInstance();

    Calendar today = Calendar.getInstance();
    int currentYear = today.get(Calendar.YEAR);
    int currentMonth = today.get(Calendar.MONTH);
    int currentDay = today.get(Calendar.DATE);

    int firstApodYear = 1995;
    int firstApodMonth = 5;
    int firstApodDay = 20;

    Random rnd = new Random();
    int newYear = rnd.nextInt(currentYear - firstApodYear + 1) + firstApodYear;

    int minMonth = 0;
    int maxMonth = 11;
    if (newYear == firstApodYear)
        minMonth = firstApodMonth;/*from  www  . jav  a 2s .  co m*/
    if (newYear == currentYear)
        maxMonth = currentMonth;
    int newMonth = rnd.nextInt(maxMonth - minMonth + 1) + minMonth;

    newCalendar.set(newYear, newMonth, 1);

    int minDay = 1;
    int maxDay = newCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (newYear == firstApodYear && newMonth == firstApodMonth)
        minDay = firstApodDay;
    if (newYear == currentYear && newMonth == currentMonth)
        maxDay = currentDay;
    int newDay = rnd.nextInt(maxDay - minDay + 1) + minDay;

    newCalendar.set(newYear, newMonth, newDay);
    return newCalendar;
}

From source file:Main.java

public static String generateToken() {
    char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();

    for (int i = 0; i < 40; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);//from www  .  j  a  va2 s.c  om
    }

    return sb.toString();
}

From source file:Main.java

public static void saveImage(Bitmap bmp) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/abcxyz");
    myDir.mkdirs();/*from w w  w . j a  v a2s  .c om*/
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String name = "Image-" + n + ".jpg";
    File file = new File(myDir, name);
    if (file.exists())
        file.delete();

    try {
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void permute(Random random, ArrayList array) {
    for (int i = 0; i < array.size(); i++) {
        int j = i + random.nextInt(array.size() - i);
        Object tmp = array.get(i);
        array.set(i, array.get(j));/* w w  w.ja  v a2s  .  c  o m*/
        array.set(j, tmp);
    }
}