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:com.clonephpscrapper.utility.FetchPageWithProxy.java

public static int generateRandomPort() {

    int portNo;//from   ww w . j  av  a  2 s.c om
    Random random = new Random();
    int[] portList = new int[98];
    int portBegin = 1601; //1601

    for (int k = 0; k < portList.length; k++) {
        portList[k] = portBegin;
        portBegin = portBegin + 1;
    }

    int num = random.nextInt(98);
    portNo = portList[num];
    return portNo;
}

From source file:com.qmetry.qaf.automation.data.BaseDataBean.java

public static String getRandomValue(String... values) {
    if ((values == null) || (values.length == 0)) {
        return "";
    }//from w w  w  . ja  v  a 2  s  .c  o m
    if ((values.length == 1) && getBundle().containsKey(values[0])) {
        values = getBundle().getStringArray(values[0], values[0]);
    }
    Random rand = new Random();
    int r = rand.nextInt(values.length);

    return values[r];
}

From source file:com.suiveg.utils.encryption.algorithms.Crypt.java

/**
 * Generate a salt based on Alpha-chars/*from  w w  w.j a  va 2  s.  c  om*/
 *
 * @return generated salt
 */
public static String generateSalt() {
    String salt = "";
    Random r = new Random();

    while (salt.length() < 2) {
        salt += alphabet.charAt(r.nextInt(alphabet.length()));
    }

    return salt;
}

From source file:com.maydesk.base.util.PDUtil.java

public static String createLogin(int charCount) {
    StringBuilder login = new StringBuilder();
    Random r = new Random();
    for (int i = 0; i < charCount; i++) {
        if (i % 3 == 0 && i != 0) {
            login.append('-');
        }/* w w w. j a  v  a 2  s.  c  o m*/
        login.append(arrayChars[r.nextInt(arrayChars.length)]);
    }
    return login.toString();
}

From source file:com.tjtolley.roborally.game.BoardDefinition.java

public static BoardDefinition randomConfiguration(String name, int height, int width) {
    Random r = new Random();
    List<List<Tile>> board = Lists.newArrayList();
    Tile.TileType[] values = Tile.TileType.values();
    final Direction[] dirValues = Direction.values();
    for (int x = 0; x < width; x++) {
        final ArrayList<Tile> column = Lists.<Tile>newArrayList();
        board.add(x, column);//from   w w  w  . j  a  v  a 2s  .  c om
        for (int y = 0; y < height; y++) {
            int valIdx = r.nextInt(values.length);
            int dirIdx = r.nextInt(dirValues.length);
            column.add(y, new Tile(values[valIdx], x, y, dirValues[dirIdx], EdgeType.EMPTY, EdgeType.EMPTY,
                    EdgeType.EMPTY, EdgeType.EMPTY));
        }
    }
    return new BoardDefinition(name, board, width, height);
}

From source file:com.github.braully.graph.DatabaseFacade.java

private static String saveConsole(List<String> consoleOut, String fileGraphName) {
    if (consoleOut == null || consoleOut.isEmpty()) {
        return null;
    }/*  w  ww .j  a v  a2s  .  c om*/
    String fileName = fileGraphName;

    char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }

    fileName = fileGraphName + ".log";
    File fileOut = new File(DATABASE_DIRECTORY_GRAPH + File.separator + fileName);
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileOut));
        for (String l : consoleOut) {
            bw.write(l);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileName;
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.LoadRRFToDB.java

public static String[] validateRRF(URI rrfLocation, boolean skipNonLexGridFiles, LgMessageDirectorIF md)
        throws Exception {
    Random r = new Random();
    // generate a random db label
    return createAndLoadTables(rrfLocation, skipNonLexGridFiles, false, "jdbc:hsqldb:mem:" + r.nextInt(100),
            "org.hsqldb.jdbcDriver", "sa", "", md, true);
}

From source file:com.github.braully.graph.DatabaseFacade.java

public static String saveGraph(UndirectedSparseGraphTO graph) {
    if (graph == null) {
        return null;
    }/*from   www  . j av  a2  s . com*/
    String fileName = graph.getName();

    char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder sb = new StringBuilder();
    //        try {
    //            MessageDigest md5 = MessageDigest.getInstance("MD5");
    //            md5.
    //        } catch (Exception e) {
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    //        }
    fileName = fileName + "-" + sb.toString() + ".json";

    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(new File(DATABASE_DIRECTORY_GRAPH + File.separator + fileName), graph);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileName;
}

From source file:Main.java

/**
 * Fills the array with random floats.  Values will be between min (inclusive) and
 * max (inclusive).//from   ww  w. j ava  2 s .  com
 */
public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            float mantissa = r.nextFloat();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            float rand = sign * mantissa * (float) Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            float rand = r.nextFloat();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        float f = (float) sInterestingDoubles[i];
        if (min <= f && f <= max) {
            array[r.nextInt(array.length)] = f;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Float.NaN;
        array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.MIN_VALUE;
        array[r.nextInt(array.length)] = Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = Float.MAX_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Float.MAX_VALUE;
    }
}

From source file:org.osiam.tests.performance.tools.TestDataCreation.java

private static ByteBuffer getBigByteBuffer(Integer countCurrentUser) {
    String userId = countCurrentUser.toString();
    byte[] bytes = new byte[userId.length() + MIN_COUNT_BYTE_BUFFER];
    int actPosition;

    // first comes the id
    char[] userChars = userId.toCharArray();
    for (int count = 0; count < userChars.length; count++) {
        bytes[count] = (byte) userChars[count];
    }//from w  w w.j av  a  2 s.  c om
    actPosition = userChars.length;

    // now we add random bytes
    Random random = new Random();
    String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyz";
    int max = allowedChars.length();
    for (int i = 0; i < MIN_COUNT_BYTE_BUFFER; i++) {
        int value = random.nextInt(max);
        bytes[actPosition++] = (byte) allowedChars.charAt(value);
    }

    ByteBuffer ret = ByteBuffer.wrap(new byte[bytes.length]);
    ret.put(bytes);
    ret.flip();

    return ret;
}