List of usage examples for java.util Random Random
public Random()
From source file:Main.java
/** * get a fixed-length random string, its a mixture of chars in sourceChar * * @param sourceChar//w w w . j a v a 2s . c om * @param length * @return <ul> * <li>if sourceChar is null or empty, return null</li> * <li>if length less than 0, return null</li> * </ul> */ public static String getRandom(char[] sourceChar, int length) { if (sourceChar == null || sourceChar.length == 0 || length < 0) { return null; } StringBuilder str = new StringBuilder(length); Random random = new Random(); for (int i = 0; i < length; i++) { str.append(sourceChar[random.nextInt(sourceChar.length)]); } return str.toString(); }
From source file:io.cortical.rest.model.TestDataMother.java
/** * Create dummy {@link Fingerprint}.//from w ww. j ava 2 s. c o m * * @return dummy fingerprint. */ public static Fingerprint createFingerprint() { Random random = new Random(); Set<Integer> positionSet = new HashSet<>(); while (positionSet.size() <= FINGERPRINT_LENGTH) { positionSet.add(random.nextInt(MAX_POSITION)); } Integer[] positionsInteger = new Integer[FINGERPRINT_LENGTH]; positionsInteger = positionSet.toArray(positionsInteger); sort(positionsInteger); return new Fingerprint(ArrayUtils.toPrimitive(positionsInteger)); }
From source file:boutique.service.GenerationCodePromoService.java
public void GenerationCodePromo(CodePromo codePromo) { Random rand = new Random(); Integer nombreAleatoire = rand.nextInt(999999 - 100000 + 1) + 100000; codePromo.setCode(nombreAleatoire.toString()); codePromoService.save(codePromo);/*from w w w . j ava 2 s . com*/ }
From source file:Main.java
public static <T> T generateRandom(Class<T> objectClass) { Random r = new Random(); if (objectClass.equals(String.class)) { String s = ""; for (int i = 0; i < 10; i++) { char c = (char) (Math.abs(r.nextInt()) % ('Z' - 'A') + 'A'); s = s + c;/*from w w w.jav a 2s . c o m*/ } return objectClass.cast(s); } else if (objectClass.equals(Integer.class)) { Integer s = r.nextInt(); return objectClass.cast(s); } else if (objectClass.equals(Long.class)) { Long s = r.nextLong(); return objectClass.cast(s); } else if (objectClass.equals(java.util.Date.class)) { 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); return objectClass.cast(c.getTime()); } return null; }
From source file:Main.java
int getRandom() { return this.range.get(new Random().nextInt(this.range.size())); }
From source file:mb.impostor.bytearray.RandByteArray.java
public byte[] randDataGen(String type, int count) { byte[] randbuf; if (type.compareToIgnoreCase("NRandom") == 0) { randbuf = RandomUtils.nextBytes(new Random().nextInt(count)); return randbuf; } else if (type.compareToIgnoreCase("Random") == 0) { randbuf = RandomUtils.nextBytes(count); return randbuf; } else if (type.compareToIgnoreCase("RandString") == 0) { String s = RandomStringUtils.randomAlphanumeric(count); return s.getBytes(); } else {//from w w w . j a va 2 s . c o m return null; } }
From source file:justdailscrapper.vik.utility.FetchPageWithoutProxy.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;//w w w . ja va 2 s. c o m 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); System.out.println("PROX FAILURE"); } 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) { System.out.println("Response content length: " + entity.getContentLength()); 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.saax.gestorweb.util.DAOAleatorio.java
public static EmpresaCliente getEmpresaClienteAleatoria(EntityManager em, Empresa e) { List<EmpresaCliente> empresaClientes = em.createNamedQuery("EmpresaCliente.findByEmpresa") .setParameter("empresa", e).getResultList(); return empresaClientes.get(new Random().nextInt(empresaClientes.size() - 1)); }
From source file:onlineBoutique.service.GenerationCodePromoService.java
public void generationDunCodePromo(CodePromo codePromo) { Random rand = new Random(); Integer max = 9999;/*from w ww . j ava 2 s . co m*/ Integer min = 1000; Integer numAleatoire = rand.nextInt(max - min + 1) + min; codePromo.setCodePromo(numAleatoire.toString()); codePromoService.save(codePromo); }
From source file:Main.java
public Main() { for (int i = 0; i < panels.length; i++) { final String[] labels = new String[] { "0", "1" }; final Random rand = new Random(); int index = rand.nextInt(labels.length); String randomTitle = labels[index]; final JLabel label = new JLabel(randomTitle, JLabel.CENTER); Timer lblt = new Timer(00, new ActionListener() { @Override/* ww w . j a v a 2 s.c om*/ public void actionPerformed(ActionEvent ae) { label.setText(labels[rand.nextInt(labels.length)]); } }); lblt.setRepeats(true); lblt.start(); label.setForeground(Color.green); label.setVerticalAlignment(JLabel.CENTER); panels[i] = new JPanel(); panels[i].setBackground(Color.BLACK); panels[i].add(label); frame.getContentPane().add(panels[i]); } frame.setLayout(new GridLayout(grid, grid)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH); frame.setVisible(true); ActionListener action = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < panels.length; i++) { Color mix = new Color(255, 255, 255); Random random = new Random(); int r = random.nextInt(255); int g = random.nextInt(255); int b = random.nextInt(255); if (mix != null) { r = (r + mix.getRed()) / 2; g = (g + mix.getGreen()) / 2; b = (b + mix.getBlue()) / 2; } Color color = new Color(r, g, b); panels[i].setBackground(color); } } }; t = new Timer(00, action); t.setRepeats(true); t.start(); }