Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

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

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

public static String generateRandomDate() {
    Random r = new Random();
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
    c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
    c.setLenient(true);//ww w  .j ava  2 s.  c  om
    return DATE_FORMAT.format(c.getTime());
}

From source file:fixio.netty.pipeline.client.SimpleFixSessionSettingsProviderTest.java

@BeforeClass
public static void setUp() {
    beginString = randomAscii(5);//from ww  w  .  j av  a2 s.c  om
    senderCompID = randomAscii(5);
    senderSubID = randomAscii(5);
    targetCompID = randomAscii(5);
    targetSubID = randomAscii(5);
    resetMsgSeqNum = true;
    heartbeatInterval = new Random().nextInt(100) + 1;

    settingsProvider = new SimpleFixSessionSettingsProvider();
    settingsProvider.setBeginString(beginString);
    settingsProvider.setSenderCompID(senderCompID);
    settingsProvider.setSenderSubID(senderSubID);
    settingsProvider.setTargetCompID(targetCompID);
    settingsProvider.setTargetSubID(targetSubID);
    settingsProvider.setResetMsgSeqNum(resetMsgSeqNum);
    settingsProvider.setHeartbeatInterval(heartbeatInterval);
}

From source file:logica.Array.java

public void llenar(int _CantidadMaxima) {
    Random rnd = new Random();
    for (int i = 0; i < _CantidadMaxima; i++) {
        array.add(rnd.nextInt(1000));//from   w w  w  . j  ava  2 s  .  c om
    }
}

From source file:justdailscrapper.vik.utility.FetchPageWithProxy.java

public static String fetchPageSourcefromClientGoogle(URI newurl, List<ProxyImport> proxyList)
        throws IOException, InterruptedException {

    Random r = new Random();

    ProxyImport obj = proxyList.get(r.nextInt(proxyList.size()));

    String ip = obj.proxyIP;//www  .j a v a  2 s.  com
    int portno = Integer.parseInt(obj.proxyPort);
    String username = "";
    String password = "";
    if (obj.proxyLen > 2) {
        username = obj.proxyUserName;
        password = obj.proxyPassword;
    }

    //        int portNo = generateRandomPort();
    CredentialsProvider credsprovider = new BasicCredentialsProvider();
    credsprovider.setCredentials(new AuthScope(ip, portno),
            new UsernamePasswordCredentials(username, password));
    HttpHost proxy = new HttpHost(ip, portno);
    //-----------------------------------------------------------------------
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).build();

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0")
            .setDefaultRequestConfig(requestConfig).setProxy(proxy).build();
    String responsebody = "";
    String responsestatus = null;
    int count = 0;
    try {
        HttpGet httpget = new HttpGet(newurl);
        httpget.addHeader("Accept", "*/*");
        httpget.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpget.addHeader("Accept-Language", "en-US,en;q=0.5");
        httpget.addHeader("Host", "www.justdial.com");
        httpget.addHeader("Referer", "https://www.justdial.com/Bangalore/Yamaha-Two-Wheeler-Dealers");
        httpget.addHeader("Connection", "keep-alive");
        httpget.addHeader("X-Requested-With", "XMLHttpReques");

        System.out.println("Response status " + httpget.getRequestLine());
        CloseableHttpResponse resp = httpclient.execute(httpget);
        responsestatus = resp.getStatusLine().toString();
        if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403")
                || responsestatus.contains("400") || responsestatus.contains("407")
                || responsestatus.contains("401") || responsestatus.contains("402")
                || responsestatus.contains("404") || responsestatus.contains("405")
                || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999")
                || responsestatus.contains("ClientProtocolException")
                || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) {
            Thread.sleep(10000);
            do {
                count++;
                responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList);
                if (responsebody == null) {
                    Thread.sleep(10000);
                    logTextArea.append("PROX FAILURE\n");
                }
                if (count > 20) {
                    Thread.sleep(1000);
                    break;
                }
            } while (responsebody == null || "".equals(responsebody));
        } else {
            HttpEntity entity = resp.getEntity();
            System.out.println(resp.getStatusLine());
            if (entity != null) {
                logTextArea.append("Response content length: " + entity.getContentLength() + "\n");
                BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    responsebody = new StringBuilder().append(responsebody).append(inputLine).toString();
                }
                // writeResponseFile(responsebody, pagename);
            }
            EntityUtils.consume(entity);
        }
    } catch (IOException | IllegalStateException e) {
        System.out.println("Exception = " + e);
        do {
            count++;
            responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList);
            if (responsebody == null) {
                System.out.println("PROX FAILURE");
            }
            if (count > 15) {
                Thread.sleep(50000);
                //                    responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
                break;
            }
        } while (responsebody == null || "".equals(responsebody));
    } finally {
        httpclient.close();
    }
    return responsebody;
}

From source file:com.zb.app.common.util.SerialNumGenerator.java

private static String getSuffix(Long id) {
    Long num = 1l;//from  w w w  .  j ava 2s . c o  m
    Random random = new Random();
    if (id != null) {
        num = id * (random.nextInt(1000));
        if (num < 100) {
            num = num * (random.nextInt());
        }
        num = (id * (random.nextInt(1000))) % 100000;// id??
    }

    StringBuffer suffixBuffer = new StringBuffer();
    suffixBuffer.append(String.valueOf(num)).append(random.nextInt(1000));
    return StringUtils.leftPad(suffixBuffer.toString(), 8, '0');
}

From source file:Business.PDU.java

private static byte[] formataPDU() {
    byte[] pdu = new byte[8];
    short num;//from w  ww. j av  a  2  s .  c o  m
    Random rng = new Random();

    num = (short) rng.nextInt(100);
    while (num == 0)
        num = (short) rng.nextInt(100);
    pdu[0] = 0; //Versao
    pdu[1] = 0; //segurana
    pdu[2] = (byte) (num & 0xff);
    pdu[3] = (byte) ((num >> 8) & 0xff);

    return pdu;
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    Rectangle clip = g.getClipBounds();
    g.setColor(new Color(new Random().nextInt()));
    g.fillRect(clip.x, clip.y, clip.width, clip.height);
}

From source file:Consumer.java

public void run() {
    Random rand = new Random();
    while (true) {
        int n = rand.nextInt();
        buffer.produce(n);/*from  w  ww.j  a  v  a 2 s  .  co  m*/
    }
}

From source file:org.sharetask.utility.SecurityUtil.java

/**
 * Generate password from alpha numerics characters in total length 8 characters. 
 * @return//from w w  w  .  java 2  s .  co m
 */
public static String generatePassword() {
    final char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    final StringBuilder sb = new StringBuilder();
    final Random random = new Random();

    for (int i = 0; i < PASSWORD_LENGT; i++) {
        final char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    return sb.toString();
}

From source file:fr.xebia.demo.wicket.blog.service.AbstractServiceTest.java

@BeforeClass
public static void setUpClass() {
    randomizer = new Random();
    long startTime = System.currentTimeMillis();
    logger.info("Initializing Services");
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URL configuration = contextClassLoader.getResource("applicationContext-service.xml");
    factory = new XmlBeanFactory(new UrlResource(configuration));
    long endTime = System.currentTimeMillis();
    logger.info("Initialisation des services en " + ((endTime - startTime) / 1000.0) + " s");
}