Java Code Examples for java.lang.Math#random()
The following examples show how to use
java.lang.Math#random() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MillerRabinTest.java From CompetitiveJava with MIT License | 6 votes |
public static boolean millerTest(int d, int n) { int a = 2 + (int)(Math.random() % (n - 4)); int x = power(a, d, n); if (x == 1 || x == n - 1) return true; while (d != n - 1) { x = (x * x) % n; d *= 2; if (x == 1) return false; if (x == n - 1) return true; } return false; }
Example 2
Source File: FermatPrimalityTest.java From CompetitiveJava with MIT License | 5 votes |
public static boolean isPrime(int n, int k) { if (n <= 1 || n == 4) return false; if (n <= 3) return true; while (k > 0) { int a = 2 + (int)(Math.random() % (n - 4)); if (power(a, n - 1, n) != 1) return false; k--; } return true; }
Example 3
Source File: BaiduFanyi.java From squirrelAI with Apache License 2.0 | 5 votes |
public static String getBaiduFanyi(String text) { int intMath = 0; String md5 = ""; String returnData = ""; try { intMath = (int) (Math.random() * 100); md5 = setMD5(appid + text + String.valueOf(intMath) + key); connection = Jsoup.connect("http://api.fanyi.baidu.com/api/trans/vip/translate"); connection.header("Content-Type", "application/json"); connection.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); connection.data("q", text); connection.data("from", "auto"); connection.data("to", "auto"); connection.data("appid", appid); connection.data("salt", String.valueOf(intMath)); connection.data("sign", md5); connection.ignoreContentType(true); connection.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"); connection.timeout(5000); document = connection.post(); String strDocJosn = document.text(); JSONObject jsonObject = JSONObject.fromObject(strDocJosn); JSONArray jsonArray = jsonObject.getJSONArray("trans_result"); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); returnData = jsonObject1.getString("dst"); } } catch (Exception e) { e.printStackTrace(); } return returnData; }
Example 4
Source File: Decomp.java From chatbot with Apache License 2.0 | 5 votes |
/** * Step to the next reassembly rule. * If mem is true, pick a random rule. */ public void stepRule() { int size = reasemb.size(); if (mem) { currReasmb = (int)(Math.random() * size); } // Increment and make sure it is within range. currReasmb++; if (currReasmb >= size) currReasmb = 0; }
Example 5
Source File: NsSampleClientThread.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Opens a connection and executes DML (insert, select, update, delete) operations */ public void doWork() { Connection conn = null; ResultSet rs = null; try { conn = getConnection(dbUrl,properties); if(conn == null) throw new Exception("Failed to obtain connection!"); conn.setAutoCommit(true); // Setting isolation level to read uncommitted, since this is a sample application. // Please set the isolation level depending on the requirements of your application setIsolationLevel(conn,Connection.TRANSACTION_READ_UNCOMMITTED); prepareStmts(conn); // Perform the DML operations for (int i=0; i<NsSample.ITERATIONS; i++) { // Choose between either a select or any one of (insert or update or delete ) operation int choice = (int) (Math.random() * 100) % 2; switch (choice) { case 0: { //select a row rs = getMaxKey.executeQuery(); //gets max t_key value long selectWhere = 0; if(rs.next()) { selectWhere = rs.getLong(1); } int numSelected = doSelectOperation(selectWhere); break; } case 1: { //do an insert, update or delete doIUDOperation(); break; } } //end of switch() }//enf of for() } catch(Exception e) { pw.println("[NsSampleWork] Thread id - "+ thread_id + "; error when performing dml operations; "); e.printStackTrace(); } finally { try { if(rs != null) rs.close(); closeConnection(conn); cleanup(); } catch(Exception ee) { pw.println("[NsSampleWork] Thread id - " + thread_id+"; error when cleaning up connection, resultset; exception is "); ee.printStackTrace(); } } }
Example 6
Source File: NsSampleClientThread.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Opens a connection and executes DML (insert, select, update, delete) operations */ public void doWork() { Connection conn = null; ResultSet rs = null; try { conn = getConnection(dbUrl,properties); if(conn == null) throw new Exception("Failed to obtain connection!"); conn.setAutoCommit(true); // Setting isolation level to read uncommitted, since this is a sample application. // Please set the isolation level depending on the requirements of your application setIsolationLevel(conn,Connection.TRANSACTION_READ_UNCOMMITTED); prepareStmts(conn); // Perform the DML operations for (int i=0; i<NsSample.ITERATIONS; i++) { // Choose between either a select or any one of (insert or update or delete ) operation int choice = (int) (Math.random() * 100) % 2; switch (choice) { case 0: { //select a row rs = getMaxKey.executeQuery(); //gets max t_key value long selectWhere = 0; if(rs.next()) { selectWhere = rs.getLong(1); } int numSelected = doSelectOperation(selectWhere); break; } case 1: { //do an insert, update or delete doIUDOperation(); break; } } //end of switch() }//enf of for() } catch(Exception e) { pw.println("[NsSampleWork] Thread id - "+ thread_id + "; error when performing dml operations; "); e.printStackTrace(); } finally { try { if(rs != null) rs.close(); closeConnection(conn); cleanup(); } catch(Exception ee) { pw.println("[NsSampleWork] Thread id - " + thread_id+"; error when cleaning up connection, resultset; exception is "); ee.printStackTrace(); } } }