Amount random allocation algorithm (modified version)

Source: Internet
Author: User

Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List;ImportJava.util.Random;/*** Amount Random allocation algorithm *@authorKWF *@since2018-2-9 11:03:59*/ Public classMain {//parameters that need to be set    Private Static Final DoubleMin_money = 0.1;//Minimum amount that the user obtains    Private Static Final DoubleMax_money = 0;//The maximum amount the user obtains (there may be an error.) Used primarily for calculation multiples, with a value of 0 using the default multiplier)    Private Static Final DoubleTotal_money = 888;//Total Amount    Private Static Final intTotal_count = 3000;//Total number of copies    Private Static Final BooleanIs_upset =true;//whether the results need to be disrupted    Private Static DoubleTimes = 10;//Multiples (maximum amount the user obtains = current mean * multiple, current average = Remaining amount/remainder) (if the maximum amount is not 0 will be re-assigned)    Private Static DoubleAvg_scale = 0.8;//tending to mean ratio    Private Static DoubleAvg_float_scale = 0.5;//The ratio of the mean to the upper and lower floats        Private Static DoubleAvgmoney = Total_money/total_count;//Average        Private Static intAvgcount = (int) Math.floor (Total_count * avg_scale);//number of copies tending to mean    Private Static DoubleRandomcount = Total_count-avgcount;//number of randomly allocated copies    Private Static DoubleLeftmoney = Total_money;//Remaining Amount    Private Static intLeftcount = Total_count;//number of remaining copies        Private Static Doubleavgtotal = 0;//mean Value list value    Private Static Doublerandomtotal = 0;//total value of the random list    Private Static intRuncount = 0;//Number of runs    Private Static intMincount = 0;//The number of minimum amounts obtained in the algorithm    Private Static intAvgbottomcount = 0;//the number of values below the mean    Private Static DoubleMaxValue, MinValue;//maximum and minimum values obtained in the algorithm        Private Staticlist<double> list =NewArraylist<> ();//used to store a list of amounts    Private Static intTreetime = 1;//recursive number of times, more than 10 direct return to the minimum value, to prevent the recursive layer too deep caused by the stack overflow         Public Static voidMain (string[] args) {System.out.println ("Multiples of" + settimes () + ", average =" +Avgmoney); if(!isright (Total_money, Total_count)) {//if the set amount and number of copies are not valid, the errorinitallavglist (); } Else{initavglist ();        Initrandomlist (); }        if(Is_upset) {System.out.println ("Upset before:" + list);//list of amounts before the print is scrambledCollections.shuffle (list);//Scramble ListSystem.out.println ("Scrambled after:" + list);//print a list of scrambled amounts}Else{System.out.println (list);//Print Amount List} System.out.println ("Total value is" + Math.Round (avgtotal) + ", random total =" +Math.Round (randomtotal)); System.out.println ("The algorithm runs" + Runcount + "Times");//Print Amount ListMaxValue= MinValue = List.get (0);  for(Doublevalue:list) {MaxValue= value > MaxValue?Value:maxvalue; MinValue= value < MinValue?Value:minvalue; } System.out.println ("Maximum value is" + MaxValue + ", minimum value is" +minValue); System.out.println ("The number of less than average is" +avgbottomcount); System.out.println ("The minimum amount is the number of" +mincount); }        /*** Populate the list of true mean values*/    Private Static voidinitallavglist () { for(inti = 0; i < Total_count; i++) {            DoubleMoney = Math.floor (Avgmoney * 100)/100;            List.add (Money); Avgtotal+=Money ; if(Money < Avgmoney) avgbottomcount++; if(Double.doubletolongbits (money) = = Double.doubletolongbits (Min_money)) mincount++; }    }        /*** Fill floating mean list*/    Private Static voidinitavglist () { for(inti = 0; i < Avgcount; i++) {            DoubleMoney =Getavgmoney ();            List.add (Money); Avgtotal+=Money ; if(Money < Avgmoney) avgbottomcount++; if(Double.doubletolongbits (money) = = Double.doubletolongbits (Min_money)) mincount++; }    }        /*** Populate random list*/    Private Static voidinitrandomlist () { for(inti = 0; i < Randomcount; i++) {            DoubleMoney =Getrandommoney ();            List.add (Money); Randomtotal+=Money ; if(Money < Avgmoney) avgbottomcount++; if(Double.doubletolongbits (money) = = Double.doubletolongbits (Min_money)) mincount++; }    }        /*** mean value up/down floating algorithm *@returnMean value (mean + mean * floating ratio)*/    Private Static DoubleGetavgmoney () {Runcount++; if(Treetime >= 10) {Treetime= 1; returnMin_money; }        if(Leftcount = = 1) {            return(Double) Math.Round (Leftmoney * 100)/100; } Random R=NewRandom (); DoubleMoney = Avgmoney + (r.nextdouble () * 2-1) * Avg_float_scale *Avgmoney; Money= Money <= Min_money?Min_money:money; Money= Math.floor (Money * 100)/100; if(Isright (Leftmoney-money, leftCount-1) ) {Treetime= 1; Leftmoney-=Money ; Leftcount--; returnMoney ; } Else{//if it is not legal, the random algorithm is called recursively until the legaltreetime++; returnGetavgmoney (); }    }        /*** Random Algorithm *@returnRandom Amount (Minimum amount ~ current mean * multiple)*/    Private Static DoubleGetrandommoney () {Runcount++; if(Treetime >= 10) {Treetime= 1; returnMin_money; }        if(Leftcount = = 1) {            return(Double) Math.Round (Leftmoney * 100)/100; } Random R=NewRandom (); Doublemax = Leftmoney/leftcount *Times ; DoubleMoney = r.nextdouble () *Max; Money= Money <= Min_money?Min_money:money; Money= Math.floor (Money * 100)/100; if(Isright (Leftmoney-money, leftCount-1) ) {Treetime= 1; Leftmoney-=Money ; Leftcount--; returnMoney ; } Else{//if it is not legal, the random algorithm is called recursively until the legaltreetime++; returnGetrandommoney (); }    }        /*** To determine if the amount and number of copies are legal and the average value is less than the minimum amount is considered illegal *@paramMoney Amount *@paramcount Number of copies *@returnlegitimacy*/    Private Static BooleanIsright (DoubleMoneyintcount) {        returnMoney/count >=Min_money; }        /*** Set multiple (only if the maximum amount is set to be valid, otherwise the default multiple) *@returnMultiples*/    Private Static DoubleSettimes () {if(Max_money! = 0) { times= Max_money/(Total_money/total_count); }        returnTimes ; }}

Amount random allocation algorithm (modified version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.