Java: Generates a random probability of 2 decimal digits, making the and 1

Source: Internet
Author: User

public static list<instsec> setdatasec (list<string> instno) {list<instsec> result = new arraylist< Instsec> (); String[] Mon ={"2014-01", "2014-02", "2014-03", "2014-04"};int[] flag ={1,2,3,4,5}; list<string> instnosub5 = new arraylist<string> (); for (int i=0;i<instno.size (); i++) {if (! Instnosub5.contains (Instno.get (i). substring (0,5))) {Instnosub5.add (Instno.get (i). substring (0,5));}} For each organvalue, there are five levels per month, corresponding to flag propfor (int i=0;i<instnosub5.size (); i++) {for (int j=0;j<mon.length;j++) { float[] prop = Genprop ();//Note: A random probability is generated here for (int k=0;k<flag.length;k++) {instsec inst=new instsec (); inst.setinst_no_ SUB5 (Instnosub5.get (i)); Inst.setsa_tx_m (Mon[j]); Inst.setflag (Flag[k]); Inst.setprop (Prop[k]); Result.add (inst);}} return result;} public static float[] Genprop () {//Generate probabilistic random number, and let and for 1float[] prop =new float[5];int[] randint =new int[5];int sum=0;for (int i=0 ; i<5;i++) {randint[i]=1+ (int) (Math.random () *10); sum+=randint[i];}
DecimalFormat df=new DecimalFormat (". xx"), float sumprop=0;for (int i=0;i<5;i++) {prop[i]= float.parsefloat ( Df.format (1.0*randint[i]/sum));//system.out.println ("Prop is" +prop[i]); sumprop+=prop[i];} System.out.println ("Sumprop is" +sumprop) sumprop=float.parsefloat (Df.format (Sumprop)); if (sumprop<1) prop[4] +=1-sumprop;elseprop[4]-=sumprop-1;//system.out.println ("prop[4] is" +prop[4]); return prop;}

Note: The subscript for arguments to SQL is starting from 1, and the Java substring method starts with 0

Java: Generates a random probability of 2 decimal digits, making the and 1

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.