forked from boboucn/boboucn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_specified.html
1 lines (1 loc) · 3.92 KB
/
random_specified.html
1
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script><style>input{font-size:medium;padding:3px;margin-top:3px}hr{width:350px}</style></head><body style="font-size: large;"><div style="text-align: center; margin-left: 20px; margin-right: 20px;"><h1 style="font-size: xx-large;">随机抽取学号</h1><div><span id="minval_title">最小值(包括):</span><input type="text"id="minval_input"></div><div style="margin-top: 10px;">最大值(包括):<input type="text"id="maxval_input"></div><hr><div style="font-size: medium; margin-top: 10px; cursor: pointer;"id="specify_title">为个别学号调整概率</div><div id="specify_form"style="display: none;"><div style="margin-top: 20px;">指定的学号:<input type="text"id="specified_id"></div><div style="margin-top: 10px;">概率调整到:<input type="text"id="specified_pos"></div><p style="font-size: small;">多个指定的学号请用空格分开<br/>“概率调整到”字段请填入一个0到1之间的小数</p></div><hr style="margin-top: 10px;"><h3 id="number_display"style="height: max-content;">学号</h3><button id="get_number"style="margin-top: 10px; padding-top: 5px; padding-bottom: 5px; padding-left: 10px; padding-right: 10px; font-size: large;">抽取</button></div><script type="text/javascript">var prevmin=0,prevmax=0;var get_random_default=function(){return window.crypto.getRandomValues(new Uint32Array(1))[0]};var getint_inrange=function(minval,maxval){return get_random_default()%(maxval-minval+1)+minval}class random_heap{constructor(minvalue,maxvalue,seed=998244353){this.minval=minvalue;this.maxval=maxvalue;this.heap=new Uint32Array(maxvalue-minvalue+2);for(var i=1;i<=maxvalue-minvalue+1;++i){this.heap[i]=minvalue+i-1}for(var i=0;i<maxvalue-minvalue;++i){var ind=getint_inrange(i,maxvalue-minvalue+1);var t=this.heap[i];this.heap[i]=this.heap[ind];this.heap[ind]=t}}getint=function(){var result=this.heap[1];var index=1,prev=0;for(;;){prev=index;index=(index<<1)|(get_random_default()&1);if(index>this.maxval-this.minval+1){break}var t=this.heap[prev];this.heap[prev]=this.heap[index];this.heap[index]=t}return result}};var rndhp;var specified_numbers=[];$(function(){if(window.localStorage.getItem("minval")!=null&&window.localStorage.getItem("maxval")!=null){$("#minval_input").val(parseInt(window.localStorage.getItem("minval")));$("#maxval_input").val(parseInt(window.localStorage.getItem("maxval")))}});$("#specify_title").click(function(){$("#specify_form").slideToggle()});$("#get_number").click(function(){var minval=$("#minval_input").val();var maxval=$("#maxval_input").val();minval=parseInt(minval);maxval=parseInt(maxval);if(isNaN(minval)||isNaN(maxval)){alert("输入内容有误,请检查格式")}else{if($("#specified_id").val().replace(" ","")!=""){specified_numbers=$("#specified_id").val().split(" ");for(var i=0;i<specified_numbers.length;++i){if(specified_numbers[i].replace(" ","")==""){specified_numbers.remove(i);continue}if(isNaN(specified_numbers[i]=parseInt(specified_numbers[i]))){alert("输入内容有误,请检查格式");return}}var probability=parseFloat($("#specified_pos").val());if(isNaN(probability)||probability<0||probability>1){alert("输入内容有误,请检查格式");return}if(get_random_default()<probability*parseFloat(1n<<32n)){$("#number_display").text(specified_numbers[get_random_default()%specified_numbers.length].toString());return}}var threshold=256,rnd=0;if(maxval-minval+1>=threshold){rnd=get_random_default()}else{if(minval!=prevmin||maxval!=prevmax){rndhp=new random_heap((prevmin=minval),(prevmax=maxval))}rnd=rndhp.getint()}rnd=rnd%(maxval-minval+1)+minval;$("#number_display").text(rnd.toString());window.localStorage.setItem("minval",minval.toString());window.localStorage.setItem("maxval",maxval.toString())}});</script></body></html>