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() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

Usage

From source file:org.ale.scanner.zotero.web.zotero.ZoteroAPIClient.java

public static String newWriteToken() {
    // Make 16 hex character write token
    Random rng = new Random();
    return Integer.toHexString(rng.nextInt()) + Integer.toHexString(rng.nextInt());
}

From source file:UUIDUtils.java

private static void appendRandomHexChars(int n, Random rand, StringBuffer result) {
    int digitsPerInt = DIGITS_PER_INT;
    while (n > 0) {
        int digitsToUse = Math.min(n, digitsPerInt);
        n -= digitsToUse;/* w ww. j  a v  a  2 s  . c o m*/
        appendHexString(rand.nextInt(), true, digitsToUse, result);
    }
}

From source file:com.cedarsoft.io.LinkUtils.java

/**
 * Creates a temporary file//from  www . j  av  a2s .  c  o m
 *
 * @param prefix    the prefix
 * @param suffix    the suffix
 * @param parentDir the parent dir
 * @return the created file
 */
@Nonnull
public static File createTempFile(@Nonnull String prefix, @Nonnull String suffix, @Nullable File parentDir) {
    Random rand = new Random();

    String parent = parentDir == null ? System.getProperty("java.io.tmpdir") : parentDir.getPath();
    DecimalFormat fmt = new DecimalFormat("#####");

    File result;
    do {
        result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix);
    } while (result.exists());
    return result;
}

From source file:ml.shifu.shifu.core.binning.DynamicBinningTest.java

public static List<NumBinInfo> createNumBinInfos(int binCnt) {
    Random rd = new Random(System.currentTimeMillis());

    List<Double> thresholds = new ArrayList<Double>(binCnt - 1);
    for (int i = 0; i < binCnt - 1; i++) {
        thresholds.add(rd.nextGaussian() * 200);
    }/*from   w w  w.  java 2s .  c  o m*/

    Collections.sort(thresholds);

    List<NumBinInfo> binInfoList = NumBinInfo.constructNumBinfo(StringUtils.join(thresholds, ':'), ':');
    for (NumBinInfo binInfo : binInfoList) {
        if (rd.nextDouble() > 0.45) {
            int total = rd.nextInt() % 1000;
            int positive = (int) (total * rd.nextDouble());
            binInfo.setTotalInstCnt(total);
            binInfo.setPositiveInstCnt(positive);
        }
    }

    return binInfoList;
}

From source file:su.fmi.photoshareclient.remote.LoginHandler.java

public static boolean register(String username, char[] password) {
    try {/* w  ww . j a v a2  s.c o  m*/
        Random randomGenerator = new Random();
        ProjectProperties properties = new ProjectProperties();
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "http://" + properties.get("socket") + properties.get("restEndpoint") + "/user/create");
        StringEntity input = new StringEntity("{\"firstName\":\"desktopUser\",\"lastName\":\"desktopUserLast\","
                + "\"username\": \"" + username + "\", \"email\": \"res" + randomGenerator.nextInt()
                + "@example.com\"," + "\"password\": \"" + new String(password) + "\"}");
        input.setContentType("application/json");
        post.setEntity(input);
        HttpResponse response = (HttpResponse) client.execute(post);
        return true;
        //                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        //                String line = "";
        //                while ((line = rd.readLine()) != null) {
        //                    System.out.println(line);
        //                }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(LoginGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LoginGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

From source file:business.model.CaptchaModel.java

/**
 *
 * @param CaptchaText//  w w w . j a  v  a2  s .  co  m
 * @return
 */
public static BufferedImage CreateCaptchaImageOld(String CaptchaText) {
    BufferedImage localBufferedImage = new BufferedImage(width, height, 1);
    try {
        Graphics2D localGraphics2D = localBufferedImage.createGraphics();
        //        List<Font> fonts = FontFactory.getListFont();
        List<Font> fonts = new ArrayList<Font>();
        if (fonts.isEmpty()) {
            fonts.add(new Font("Nimbus Roman No9 L", 1, 30));
            fonts.add(new Font("VN-NTime", 1, 30));
            fonts.add(new Font("DT-Times", 1, 30));
            fonts.add(new Font("Times New Roman", 1, 30));
            fonts.add(new Font("Vni-Book123", 1, 30));
            fonts.add(new Font("VNI-Centur", 1, 30));
            fonts.add(new Font("DT-Brookly", 1, 30));
        }
        //        fonts.add(loadFont("BINHLBI.TTF"));
        RenderingHints localRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        localRenderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        localGraphics2D.setRenderingHints(localRenderingHints);
        localGraphics2D.fillRect(0, 0, width, height);
        localGraphics2D.setColor(new Color(50, 50, 50));
        Random localRandom = new Random();
        int i = 0;
        int j = 0;
        for (int k = 0; k < CaptchaText.length(); k++) {
            i += 25 + Math.abs(localRandom.nextInt()) % 5;
            j = 25 + Math.abs(localRandom.nextInt()) % 20;
            int tmp = localRandom.nextInt(fonts.size() - 1);
            localGraphics2D.setFont(fonts.get(tmp));

            localGraphics2D.drawChars(CaptchaText.toCharArray(), k, 1, i, j);
        }
        localBufferedImage = getDistortedImage(localBufferedImage);
        Graphics2D localGraphics2D2 = localBufferedImage.createGraphics();
        localGraphics2D2.setRenderingHints(localRenderingHints);
        //        localGraphics2D2.fillRect(0, 0, width, height);
        localGraphics2D2.setColor(new Color(50, 50, 50));
        CubicCurve2D c = new CubicCurve2D.Double();// draw QuadCurve2D.Float with set coordinates
        for (int l = 0; l < 7; l++) {
            int x1 = Util.rand(0, width / 2);
            ;
            int x2 = Util.rand(width / 2, width);
            int y1 = Util.rand(0, height);
            int y2 = Util.rand(0, height);
            int ctrlx1 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly1 = y1;
            int ctrlx2 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly2 = y2;

            c.setCurve(x1, y1, ctrlx2, ctrly2, ctrlx1, ctrly1, x2, y2);
            localGraphics2D2.draw(c);
            //            localGraphics2D2.drawLine(randomX1(), randomY1(), randomX2(), randomY2());
        }

        localGraphics2D.dispose();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return null;
    }
    return localBufferedImage;
}