Example usage for java.lang Math random

List of usage examples for java.lang Math random

Introduction

In this page you can find the example usage for java.lang Math random.

Prototype

public static double random() 

Source Link

Document

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 .

Usage

From source file:gov.jgi.meta.hadoop.input.FastaBlockLineReader.java

public static void main(String[] args) {

    int num = 1;/*from   w  ww .  j a v  a  2  s  . c o m*/
    int last = -1;

    do {
        try {
            FileInputStream fstream = new FileInputStream("/scratch/karan/30mb.fas");
            FastaBlockLineReader fblr = new FastaBlockLineReader(fstream);

            Text key = new Text();
            Map<String, String> setofreads = new HashMap<String, String>();
            Map<String, String> setofreadsTotal = new HashMap<String, String>();
            int length = (int) (Math.random() * 10000);
            length = 3000000;
            System.out.println("lenght = " + length);

            int total = 0;

            fblr.readLine(key, setofreads, Integer.MAX_VALUE, length);
            //            System.out.println("setofreads.size = " + setofreads.size());
            total += setofreads.size();
            //for (String s : setofreads.keySet()) {
            //                System.out.println(s);
            //            }
            Runtime r = Runtime.getRuntime();
            while (setofreads.size() > 0) {
                setofreadsTotal.putAll(setofreads);
                setofreads.clear();
                fblr.readLine(key, setofreads, Integer.MAX_VALUE, length);
                //              System.out.println("setofreads.size = " + setofreads.size());
                total += setofreads.size();

                r.gc();
            }
            System.out.println("total = " + total);
            System.out.println("heap size = " + r.totalMemory() / 1048576);

            if (last != -1) {
                if (last != total) {
                    System.out.println(
                            "error!!!, length = " + length + ": last = " + last + " current = " + total);
                }
            }
            last = total;

        } catch (Exception e) {
            System.out.println(e);
        }
    } while (num-- > 0);

}

From source file:Main.java

/**
 * This method generates a random n digit password, which contains at least one number, lower case alphabet, upper
 * case alphabet and as special character.
 *//*from   w ww.jav a2  s.c  o  m*/
public static String generatePassword(int n) {

    Random rd = new Random();

    char lowerChars[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char upperChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    char numbers[] = "0123456789".toCharArray();
    char specialChars[] = "~!@#$%^&*()-_=+[{]}|;:<>/?".toCharArray();

    List<Character> pwdLst = new ArrayList<Character>();
    for (int g = 0; g < 4; g++) {
        for (int z = 0; z < 1; z++) {
            if (g == 0) {
                pwdLst.add(numbers[rd.nextInt(10)]);
            } else if (g == 1) {
                pwdLst.add(lowerChars[rd.nextInt(26)]);
            } else if (g == 2) {
                pwdLst.add(upperChars[rd.nextInt(26)]);
            } else if (g == 3) {
                pwdLst.add(specialChars[rd.nextInt(26)]);
            }
        }
        if (pwdLst.size() == n) {
            break;
        }
        if (g + 1 == 4) {
            g = (int) Math.random() * 5;

        }
    }
    StringBuilder password = new StringBuilder();
    Collections.shuffle(pwdLst);
    for (int c = 0; c < pwdLst.size(); c++) {
        password.append(pwdLst.get(c));
    }
    return password.toString();
}

From source file:Main.java

public String getAlphaNumeric(int len) {
    StringBuffer sb = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        int ndx = (int) (Math.random() * ALPHA_NUM.length());
        sb.append(ALPHA_NUM.charAt(ndx));
    }// ww w.j  a  va  2  s .c o  m
    return sb.toString();
}

From source file:Main.java

public static int[] shuffle(int[] arr) {
    for (int i = arr.length - 1; i >= 0; --i) {
        // swap indexes
        int r = (int) Math.rint(Math.random() * i);
        int t = arr[i];
        arr[i] = arr[r];//w w  w.j av a 2 s  .  c o  m
        arr[r] = t;
    }

    return arr;
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

public static int generateRandomNumber() {
    return (int) Math.round(1 + (Math.random() * 1000000));
}

From source file:br.on.daed.services.pdf.DadosMagneticos.java

public static byte[] gerarPDF(String ano, String tipo)
        throws IOException, InterruptedException, UnsupportedOperationException {

    File tmpFolder;/*from   w w w . ja  va2  s  .c  o  m*/
    String folderName;

    Double anoDouble = Double.parseDouble(ano);
    List<String> dados = Arrays.asList(TIPO_DADOS_MAGNETICOS);

    byte[] ret = null;

    if (anoDouble >= ANO_MAGNETICO[0] && anoDouble < ANO_MAGNETICO[1] && dados.contains(tipo)) {

        do {

            folderName = "/tmp/dislin" + Double.toString(System.currentTimeMillis() * Math.random());
            tmpFolder = new File(folderName);

        } while (tmpFolder.exists());

        tmpFolder.mkdir();

        ProcessBuilder processBuilder = new ProcessBuilder("/opt/declinacao-magnetica/./gerar", ano, tipo);

        processBuilder.directory(tmpFolder);

        processBuilder.environment().put("LD_LIBRARY_PATH", "/usr/local/dislin");

        Process proc = processBuilder.start();

        proc.waitFor();

        ProcessHelper.outputProcess(proc);

        File arquivoServido = new File(folderName + "/dislin.pdf");

        FileInputStream fis = new FileInputStream(arquivoServido);

        ret = IOUtils.toByteArray(fis);

        processBuilder = new ProcessBuilder("rm", "-r", folderName);

        tmpFolder = new File("/");

        processBuilder.directory(tmpFolder);

        Process delete = processBuilder.start();

        delete.waitFor();

    } else {
        throw new UnsupportedOperationException("Entrada invlida");
    }
    return ret;
}

From source file:com.puppetlabs.geppetto.forge.v2.api.it.ReleaseTestCreate.java

@BeforeClass
public static void initialize() {
    String rnd = "000" + Integer.toString((int) (Math.random() * 10000));
    testModule = ForgeAPITestBase.TEST_MODULE_PFX + rnd.substring(rnd.length() - 4);
}

From source file:org.jfree.chart.demo.GridBandDemo1.java

public static JPanel createDemoPanel() {
    XYSeries xyseries = new XYSeries("Random Data");
    for (int i = 0; i < 100; i++)
        xyseries.add(Math.random() + 1.0D, Math.random() + 1.0D);

    XYSeriesCollection xyseriescollection = new XYSeriesCollection();
    xyseriescollection.addSeries(xyseries);
    JFreeChart jfreechart = createChart(xyseriescollection);
    ChartPanel chartpanel = new ChartPanel(jfreechart);
    return chartpanel;
}

From source file:org.jfree.chart.demo.DeviationRendererDemo2.java

private static XYDataset createDataset() {
    YIntervalSeries series1 = new YIntervalSeries("Series 1");
    YIntervalSeries series2 = new YIntervalSeries("Series 2");
    Object obj = new Week();
    double d = 100D;
    double d1 = 100D;
    for (int i = 0; i <= 52; i++) {
        double d2 = 0.050000000000000003D * (double) i;
        series1.add(((RegularTimePeriod) (obj)).getFirstMillisecond(), d, d - d2, d + d2);
        d = (d + Math.random()) - 0.45000000000000001D;
        double d3 = 0.070000000000000007D * (double) i;
        series2.add(((RegularTimePeriod) (obj)).getFirstMillisecond(), d1, d1 - d3, d1 + d3);
        d1 = (d1 + Math.random()) - 0.55000000000000004D;
        obj = ((RegularTimePeriod) (obj)).next();
    }//  ww w  . j a v a 2  s  .  co  m

    YIntervalSeriesCollection dataset = new YIntervalSeriesCollection();
    dataset.addSeries(series1);
    dataset.addSeries(series2);
    return dataset;
}

From source file:org.jfree.chart.demo.CompassFormatDemo1.java

private static XYDataset createDirectionDataset(int i) {
    TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
    TimeSeries timeseries = new TimeSeries("Wind Direction");
    Object obj = new Minute();
    double d = 180D;
    for (int j = 0; j < i; j++) {
        timeseries.add(((RegularTimePeriod) (obj)), d);
        obj = ((RegularTimePeriod) (obj)).next();
        d += (Math.random() - 0.5D) * 15D;
        if (d < 0.0D) {
            d += 360D;/*from  ww  w .  ja va 2  s.c o m*/
            continue;
        }
        if (d > 360D)
            d -= 360D;
    }

    timeseriescollection.addSeries(timeseries);
    return timeseriescollection;
}