1.首先我们进行抽红包概率测试的时候,为了测试抽取红包的概率完全正确。个人建议不使用线程池多线程执行,建议直接使用for循环执行
2.我们调取抽红包接口返回的信息值进行判断
3.下面就是判断代码,然后是抽红包接口代码
@Test
public static void test1(){
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
int h=0;
int zong=1000;
for (int i = 0; i < zong; i++) {
Double aDouble = testRequestUn();
if (aDouble != null) {
if (aDouble==1.8){
a++;
}else if (aDouble==2.8){
b++;
}else if (aDouble==3.8){
c++;
}else if (aDouble==4.8){
d++;
}else if (aDouble==5.8){
e++;
}else if (aDouble==6.8){
f++;
}else if (aDouble==7.8){
g++;
}else if (aDouble==8.8){
h++;
}
}
}
int asd=a+b+c+d+e+f+g+h;
double a1= (double) a / zong;
double b1= (double) b / zong;
double c1= (double) c / zong;
double d1= (double) d / zong;
double e1= (double) e / zong;
double f1= (double) f / zong;
double g1= (double) g / zong;
double h1= (double) h / zong;
System.out.println("总和"+asd);
System.out.println(a+"==="+b+"==="+c+"==="+d+"==="+e+"==="+f+"==="+g+"==="+h);
System.out.println("1.8占比"+a1+"%"+"=="+"2.8占比"+b1+"%"+"=="+"3.8占比"+c1+"%"+"=="+"4.8占比"+d1+"%"+"=="
+"5.8占比"+e1+"%"+"=="+"6.8占比"+f1+"%"+"=="+"7.8占比"+g1+"%"+"=="+"8.8占比"+h1+"%");
}
示例:模拟抽红包接口代码
private static Double testRequestUn() {
//编写自己的抽红包接口和返回值
Random random = new Random();
int i1 = random.nextInt(8);
Double asd = null;
Double[] arr={1.8,2.8,3.8,4.8,5.8,6.8,7.8,8.8};
asd= arr[i1];
return asd;
}
抽红包接口,但是不管用哦,地址什么的都要改成自己的
package com.znzdh.jiekou;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.logging.Logger;
public class Dalor {
@Test
public Double choujiang() throws InterruptedException {
Logger log = Logger.getLogger("choujiang");
log.info("创建一个可关闭的httpclient客户端");
CloseableHttpClient aDefault = HttpClients.createDefault();
log.info("设置接口地址");
String url="http://192.168.31.125:8081/liandongApplet/app/prize/luckDraw";//测试环境
Thread.sleep(1000);
log.info("创建一个httppost");
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type","application/json; charset=utf-8");
httpPost.addHeader("Authorization","App Bearer 7a7118b8-96d1-49c4-acde-5b2b885f62ef");
ArrayList<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("merchantId","125000610470322176"));
//把参数集中设置到urlEncodedformEntity中
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
httpPost.setEntity(formEntity);
//==============================
Thread.sleep(1000);
//把formEntity值设置到httppost对象中
//httpPost.setEntity(stringEntity);
Thread.sleep(1000);
//======================
CloseableHttpResponse response = null;
try{
//获取客户端的.execut(httppost),返回一个response
response=aDefault.execute(httpPost);
Thread.sleep(1000);
//获取response分润getEntity类
HttpEntity entity = response.getEntity();
//使用Entityuntils的工具类转换成toString设置编码
String sen = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(sen);
/*
上面entity的值有一个是需要返回的,那么把他放到下面return中进行返回
*/
//释放资源
EntityUtils.consume(entity);
}catch (Exception | Error e){
e.printStackTrace();
}finally {
//关闭可关闭的
if (aDefault != null){
try {
aDefault.close();
}catch (Exception e){
e.printStackTrace();
}
}
//关闭可关闭的response
if (response != null){
try {
response.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}