java.lang.Math#random ( )源码实例Demo

下面列出了java.lang.Math#random ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: CompetitiveJava   文件: MillerRabinTest.java
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; 
   }
 
源代码2 项目: CompetitiveJava   文件: FermatPrimalityTest.java
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; 
}
 
源代码3 项目: squirrelAI   文件: BaiduFanyi.java
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;

    }
 
源代码4 项目: chatbot   文件: Decomp.java
/**
 *  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;
}
 
源代码5 项目: gemfirexd-oss   文件: NsSampleClientThread.java
/**
 *  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();
      }
      }
}
 
源代码6 项目: gemfirexd-oss   文件: NsSampleClientThread.java
/**
 *  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();
      }
      }
}