This article shares the specific code of js to implement the specified red envelope order and amount for your reference. The specific content is as follows Preface
Effect displayEmpty package problemRed Envelope Algorithm/* param: float, int, int, float param1: total amount of red envelopes param2: number of red envelopes param3: specify special red envelopes param4: specify special red envelope amount*/ let getPrize = function(total, number, index, volume){ let allowance = total - volume; let arr = []; let i = 0; while(i < number - 2){ //Specify [0.01, allowance-(i*0.01)) let temp = (Math.random()*(allowance - (number - 1 - i) * 0.01) + 0.01).toFixed(2); // if (temp < 0) console.log(`temp:${temp}`); temp = temp <= 0 ? 0.01 : temp; arr.push(parseFloat(temp)); allowance = parseFloat((allowance - temp).toFixed(2)); i++; // console.log(`arr:${arr}, i:${i}`); // If the distribution is less than 0, exploit the strong and divide it equally if(allowance <= 0){ // console.log(`alowance:${allowance}`); let w = arr.filter((val,index)=>{ // console.log(`val:${val}`); if(val > 0.01){ arr[index] = parseFloat((arr[index] - 0.01).toFixed(2)); return val; } }); if(w.length == 0){ allowance = 0; }else{ allowance = 0.01; } } } // The last one is put in arr.push(parseFloat(allowance.toFixed(2))); let result = arr; return result.slice(0, index).concat(parseFloat(volume), result.slice(index)); } Test Examples // Test sample for(let m = 0; m < 10000; m++){ let total = (Math.random()*100 + 0.01).toFixed(2); let number = Math.floor(Math.random()*20 +2); while(total / number < 0.01){ number = Math.floor(Math.random()*20 +2); } let index = Math.floor(Math.random()*(number - 1)); let volume = (Math.random()*(total - 0.01*(number-1))+0.01).toFixed(2); while(volume >= total || volume + 0.01*(number-1) > total){ // console.log(`xx:${volume}`); volume = (Math.random()*(total - 0.01*(number-1))+0.01).toFixed(2); volume = volume <= 0 ? 0.01 : volume; } let test = getPrize(total, number, index, volume); // console.log(test); let sum = test.reduce((total,val)=>total+=parseFloat(val)); sum = sum.toFixed(2); if(sum !== total) { console.log(`volume:${volume}, total: ${total}, number: ${number}`); console.log(`sum:${sum}`); console.log(test); } test.map((val,index)=>{ if(val <= 0 && index !== test.length - 1){ console.log(`volume:${volume}, total: ${total}, number: ${number}`); console.log(`sum:${sum}`); console.log(test); } }); } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: A brief discussion on the efficiency of MySQL subquery union and in
>>: Example of using supervisor to manage nginx+tomcat containers
The installation information on the Internet is u...
Table of contents 1. Basic Concepts 2. Developmen...
1. HBase Overview 1.1 What is HBase HBase is a No...
When installing Tomcat in Docker, Tomcat may over...
Table of contents Small but beautiful Keep it sim...
MySQL is easy to install, fast and has rich funct...
Table of contents Base Return Type String and Boo...
Introduction: This article mainly introduces how ...
Win2008 R2 zip format mysql installation and conf...
Summarize Global environment ➡️ window Normal fun...
There are the following log files in MySQL: 1: re...
1. Global Object All modules can be called 1) glo...
This article shares the specific code of Vue to i...
MySQL v5.7.19 official version (32/64 bit install...
CSS Houdini is known as the most exciting innovat...