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:eu.europeana.portal.portal2.speedtests.TermProvider.java

public static List<String> getRandomWords(int count) {

    List<String> queries = null;
    try {//  w w w .  j a v a 2s .  c om
        queries = FileUtils.readLines(new File("/home/peterkiraly/words.txt"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<String> random = new ArrayList<String>();
    String query;
    while (random.size() < count) {
        int startRecord = (int) (Math.random() * queries.size());
        query = queries.get(startRecord).trim().replaceAll("[\"'()]", "").replace("/", "\\/");
        if (query.length() >= 3 && !random.contains(query)) {
            random.add(query);
        }
    }

    return random;
}

From source file:gov.nih.nci.caintegrator.application.zip.FileNameGenerator.java

public static String generateFileName(String userName) throws ValidationException {
    String fileName = null;/*from  w  w  w.j  a v a2  s.c  o m*/
    if (userName != null) {
        // Get a random number of up to 7 digits      
        int sevenDigitRandom = new Double(Math.random() * 10000000).intValue();

        // Get the last 7 digits of the Java timestamp
        String s = String.valueOf(System.currentTimeMillis());
        String lastSevenOfTimeStamp = s.substring(s.length() - 7, s.length());

        // Put it all together
        fileName = userName + "_" + sevenDigitRandom + lastSevenOfTimeStamp;
    } else {
        throw new ValidationException("Invalid Email Address");
    }
    return fileName;
}

From source file:Main.java

/**
 * A convenience method that generates a XOR key pair for a given key. It was used to generate
 * the key for {@link MainActivity#useXorStringHiding(String)} method.
 *
 * @param key The source key to use in generating the XOR key halves
 * @return a two-value string array containing both parts of the XOR key
 *//*from  w ww.j a  v  a 2  s. c  o m*/
public static String[] generateKeyXorParts(String key) {
    String[] keyParts = new String[2];

    byte[] xorRandom = new byte[key.length()];
    byte[] xorMatch = new byte[key.length()];
    byte[] keyBytes = key.getBytes();
    for (int i = 0; i < key.length(); i++) {
        xorRandom[i] = (byte) (256 * Math.random());
        xorMatch[i] = (byte) (xorRandom[i] ^ keyBytes[i]);
    }
    keyParts[0] = Base64.encodeToString(xorRandom, 0);
    keyParts[1] = Base64.encodeToString(xorMatch, 0);
    Log.i(TAG, "XOR Key Part 0: " + keyParts[0]);
    Log.i(TAG, "XOR Key Part 1: " + keyParts[1]);

    return keyParts;
}

From source file:Main.java

public static <T> T random(T[] array) {
    return array[(int) Math.floor(Math.random() * array.length)];
}

From source file:Main.java

public static int randRange(int min, int max) {
    return min + (int) (Math.random() * ((max - min) + 1));
}

From source file:ttma.client.GUI.StatOperations.java

private static PieDataset createDataset() {
    int i = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    int i1 = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    int i2 = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    int i3 = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    int i4 = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    int i5 = 100 + (int) (Math.random() * ((2000 - 100) + 1));
    DefaultPieDataset dataset = new DefaultPieDataset();
    // GestionConge g = new GestionConge();
    //dataset.setValue( "  CONGE ACCEPTE   :  '"+g.countCongeAccpte()+"'" ,g.countCongeAccpte());  
    //dataset.setValue( " CONGE REFUSE   :  '"+g.countCongeRefuse()+"'"  ,g.countCongeRefuse() );   
    //dataset.setValue( "  CONGE SANS REPONSE   :  '"+g.countCongeSansRep()+"'" ,g.countCongeSansRep());  
    dataset.setValue("  Face Surgery   :", i);
    dataset.setValue("  Breast Surgery   :  ", i1);
    dataset.setValue("  Surgery teeth   ", i2);
    dataset.setValue("  Surgery plastic  ", i3);
    dataset.setValue("  intimate Surgery  ", i4);
    dataset.setValue("  gastric Band  ", i5);

    return dataset;
}

From source file:Main.java

public static String generateNickname() {
    Random rnd = new Random(System.currentTimeMillis());
    int race_selection = rnd.nextInt(3);

    String thefirstname = "";
    String thelastname = "";
    String thefirstname1 = "";
    String thelastname1 = "";

    if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);
        double lnprefix1 = Math.floor(Math.random() * 67);
        double lnsuffix1 = Math.floor(Math.random() * 64);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);
        thelastname = h_lnpre.get((int) lnprefix1) + h_lnsuf.get((int) lnsuffix1);

        thefirstname1 = thefirstname.substring(0, 1).toUpperCase();
        thefirstname = thefirstname1 + thefirstname.substring(1, thefirstname.length());

        thelastname1 = thelastname.substring(0, 1).toUpperCase();
        thelastname = thelastname1 + thelastname.substring(1, thelastname.length());

    } else if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 80);
        double fnsuffix1 = Math.floor(Math.random() * 67);

        thefirstname = o_fnpre.get((int) fnprefix1) + o_fnsuf.get((int) fnsuffix1);
        thelastname = "";

    } else {/* w  ww  .j a  v a 2s. c o m*/
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);

    }

    return thefirstname + " " + thelastname;
}

From source file:Main.java

public static boolean isAnimatable(View view) {
    return Math.random() >= 0;
}

From source file:Main.java

/**
 * Will return a list of random location from the given position & radius
 * @param amount/*from w  w w .jav a  2s  .  c  o  m*/
 * @param lat
 * @param lng
 * @param radius
 * @return ArrayList<Location> 
 */
public static ArrayList<Location> randomLocations(int amount, double lat, double lng, double radius) {
    ArrayList<Location> locations = new ArrayList<Location>(0);
    Location centerLocation = new Location("local");
    centerLocation.setLatitude(lat);
    centerLocation.setLongitude(lng);

    final double lngScale = Math.cos((Math.PI / 180.0) * lat);
    final double radiusDeg = radius / 111.2; // radius converted to degrees (square)

    while (locations.size() < amount) {
        Location l = new Location("local");
        double dLat = (Math.random() * radiusDeg * 2) - radiusDeg;
        double dLng = (Math.random() * radiusDeg * 2 / lngScale) - (radiusDeg / lngScale);

        l.setLatitude(centerLocation.getLatitude() + dLat);
        l.setLongitude(centerLocation.getLongitude() + dLng);
        double dist = l.distanceTo(centerLocation) / 1000.0;

        if (dist < (radius)) {
            locations.add(l);
        }
    }
    return locations;
}

From source file:Main.java

private static String generatePrefixForNamespace(String namespaceURI) {
    return "ns" + (int) (Math.random() * 1000);
}