美团抽奖规则:每日收盘时的上证指数 × 每日收盘时的深证成指 × 10000 = 12位数。(指数以证交所公布数字为准);将此12位数的数字倒序排列后(如首位是0,则直接抹去),再除以前一日23:59:59本次活动结束时的参与人数(每个抽奖号为一个人数),得到的余数加1即为获奖号码;若公布时您的“抽奖号”与获奖号码完全一致,就可以获得本次活动的大奖了。
例如
2012-06-11收盘时的上证指数是 2927.08,
2012-06-11收盘时的深证成指是 12926.61,
将这两个数字相乘后再乘以10000得到的12位数为378372215988,
将这12位数的数字倒序排列得到889512273873。
如果截止至2012-06-10 23:59:59活动结束时的抽奖人数为1907899,以889512273873整除以1907899,商为466226,余数为154699,用154699+1=154700,获奖即为154700,如果当天需要开多个奖,则用此号码分别+2011 +3011 +4011……得出当天其他获奖号码。
Java代码实现
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
@Slf4j
public class StockUtil {
public static void main(String[] args) {
String sh = "sh000001";
String sz = "sz399001";
String shIndex = stockMarketIndex(sh);
String szIndex = stockMarketIndex(sz);
log.info("上证指数当前为:{}", shIndex);
log.info("深证成指当前为:{}", szIndex);
//未来某天的:上证指数 * 深证指数 * 10000,得到数字字符串然后反转
BigDecimal decimal1 = new BigDecimal(shIndex);
BigDecimal decimal2 = new BigDecimal(szIndex);
String str1 = decimal1.multiply(decimal2).multiply(new BigDecimal("10000")).setScale(0).toString();
String str2 = StringUtils.reverse(str1);
Integer peopleNum = 1785;
log.info("上证指数 * 深证指数 * 10000,得到数字字符串然后反转:{}", str2);
log.info("参加人数:{},幸运号码:{}", peopleNum, (Long.valueOf(str2)%peopleNum + 1));
}
/**
* 根据股票代码stockCode获取当前价
* */
public static String stockMarketIndex(String stockCode){
String url = "http://qt.gtimg.cn/q=" + stockCode;
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url, String.class);
log.info(response);
String[] strings = StringUtils.split(response, "=");
log.info(strings[1]);
String[] details = StringUtils.split(strings[1], "~");
return details[3];
}
}
输出:
20:23:56.672 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://qt.gtimg.cn/q=sh000001
20:23:56.672 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
20:23:56.719 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
20:23:56.719 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "text/html;charset=GBK"
20:23:56.719 [main] INFO com.wangan.http.StockUtil - v_sh000001="1~上证指数~000001~3024.39~3041.20~3042.17~204115336~102057668~102057668~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~~20220930155940~-16.81~-0.55~3054.61~3021.93~3024.39/204115336/240262768409~204115336~24026277~0.47~11.85~~3054.61~3021.93~1.07~377071.89~480720.18~0.00~-1~-1~0.85~0~3036.39~~~~~~24026276.8409~0.0000~0~ ~ZS~-16.91~-2.07~~~~3708.94~2863.65~-3.26~-5.04~-10.11~4077465388940~~-6.09~-15.88~4077465388940~~~-15.24~-0.02~";
20:23:56.719 [main] INFO com.wangan.http.StockUtil - "1~上证指数~000001~3024.39~3041.20~3042.17~204115336~102057668~102057668~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~~20220930155940~-16.81~-0.55~3054.61~3021.93~3024.39/204115336/240262768409~204115336~24026277~0.47~11.85~~3054.61~3021.93~1.07~377071.89~480720.18~0.00~-1~-1~0.85~0~3036.39~~~~~~24026276.8409~0.0000~0~ ~ZS~-16.91~-2.07~~~~3708.94~2863.65~-3.26~-5.04~-10.11~4077465388940~~-6.09~-15.88~4077465388940~~~-15.24~-0.02~";
20:23:56.735 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://qt.gtimg.cn/q=sz399001
20:23:56.735 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
20:23:56.750 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
20:23:56.750 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "text/html;charset=GBK"
20:23:56.750 [main] INFO com.wangan.http.StockUtil - v_sz399001="51~深证成指~399001~10778.61~10919.44~10925.01~283223132~141611566~141611566~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~~20220930161442~-140.83~-1.29~10962.22~10777.91~10778.61/283223132/320578275698~283223132~32057828~1.30~34.21~~10962.22~10777.91~1.69~252307.99~320273.28~0.00~-1~-1~0.87~0~10862.05~~~~~~32057827.5698~0.0000~0~ ~ZS~-27.45~-2.07~~~~15288.65~10087.53~-4.29~-7.97~-16.68~2170125102508~~2.87~-25.80~2170125102508~~~-24.67~-0.01~";
20:23:56.750 [main] INFO com.wangan.http.StockUtil - "51~深证成指~399001~10778.61~10919.44~10925.01~283223132~141611566~141611566~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~~20220930161442~-140.83~-1.29~10962.22~10777.91~10778.61/283223132/320578275698~283223132~32057828~1.30~34.21~~10962.22~10777.91~1.69~252307.99~320273.28~0.00~-1~-1~0.87~0~10862.05~~~~~~32057827.5698~0.0000~0~ ~ZS~-27.45~-2.07~~~~15288.65~10087.53~-4.29~-7.97~-16.68~2170125102508~~2.87~-25.80~2170125102508~~~-24.67~-0.01~";
20:23:56.750 [main] INFO com.wangan.http.StockUtil - 上证指数当前为:3024.39
20:23:56.750 [main] INFO com.wangan.http.StockUtil - 深证成指当前为:10778.61
20:23:56.750 [main] INFO com.wangan.http.StockUtil - 上证指数 * 深证指数 * 10000,得到数字字符串然后反转:979202789523
20:23:56.750 [main] INFO com.wangan.http.StockUtil - 参加人数:1785,幸运号码:589
引申
这个机制可以用在区块链预言机上,通过股指这种随机不可预测但事后又可验证的随机噪声来作为随机种子,来产生随机数,基于这个随机数来抽奖。
然后上面的这个算法其实可以改进一下,能不能根据所谓hash相似程度来判定哪个用户的hash中奖呢?
参考
史上最公正的抽奖算法在此!看懂这篇,年会抽奖就不用CTO现场审核代码啦
标签:基于,抽奖,20,String,23,0.00,RestTemplate,main,美团 From: https://www.cnblogs.com/lyhero11/p/16754448.html