Turntable draw Java Back-end code __java

Source: Internet
Author: User
Tags object object

Now the turntable lottery is an endless stream of activities. I've done one recently. Of course, here is a simple example of implementation, can be based on our needs to add additional content.

The way to do this is to do a random number algorithm based on the probability we set, to get our prizes. The following is the code to see directly.

Import Java.math.BigDecimal;
Import java.util.ArrayList;

Import java.util.List;

Import Com.thinkive.base.jdbc.DataRow;  
        public class Turntable {static object[][] Prizearr = new object[][]{//Inside pointer rotation {1, "First Prize", 0.0001}, {2, "Don't Lose Heart", 39.9994}, {3, "good Luck", 20.0}, {4, "Second Prize", 0.0002}, {5, "persistent", 20.0}, {6
	, "Fortune saved First", 10.0}, {7, "Third Prize", 0.0003}, {8, "Thank you for participating", 10.0}}; Object result[] = Award (Prizearr)//After draw returns the prize rank and the prize description///After the lottery returns the prize rank and the prize description public static object[] Award (object[][) Prizearr  
	    ) {//probability array float obj[] = new Float[prizearr.length];  
	    for (int i=0;i<prizearr.length;i++) {Obj[i] = Float.parsefloat (prizearr[i][2].tostring ()); Integer Prizeid = Getrand (obj);
	    Get award number according to probability String msg = (string) prizearr[prizeid][1];//prize information Integer prizeids = Prizeid +1;  
	      
	    SYSTEM.OUT.PRINTLN ("Prize Number:" +prizeids + "award Grade:" +msg);  
	return new object[]{prizeid,msg};///According to probability get awards public static Integer Getrand (float obj[]) {integer result = null; try {Float sum = total probability precision of 0.0f;//probability array float min = 0.0f;//for (int I=0;i<obj.leng
            	th;i++) {BigDecimal beforsum = new BigDecimal float.tostring (sum);
            	BigDecimal ObjValue = new BigDecimal (float.tostring (obj[i));
            sum = Beforsum.add (ObjValue). Floatvalue (); for (int i=0;i<obj.length;i++) {//probability array loop BigDecimal db = new BigDecimal (Math.random () *
            	um-min) + min);
                BigDecimal B = New BigDecimal (float.tostring (obj[i));  
                    if (CompareMethod (db,b) = = 1) {//Jackpot result = i;  
                Break  
                }else{sum-=obj[i];  
        A catch (Exception e) {e.printstacktrace ());
        /** * Prize control Code, here you can control the level of our prizes * **/return result; public static void Main (string[] args) {DataRow data = new DataRow ()//here DataRow as encapsulated HashMap List<dataro
		w> resultlist = new arraylist<datarow> ();
			for (int i = 0; I < 10000 i++) {int k = 1;
			Object result[] = Award (Prizearr);//after draw return angle and prize rank String prize = "";
			String comment = "";
				for (Object Object:result) {if (k==1) {prize = object.tostring ();
				}else if (k==2) {comment = object.tostring ();
			} k++;
				} if (k==3) {Data.set ("prize", integer.valueof (prize) +1);
				Data.set ("comment", comment);
			Resultlist.add (data);
	}} public static int CompareMethod (BigDecimal a,bigdecimal b) {return a.compareto (b); }
}
First, let's take a look at our prize initialization, which is the code.

Static object[][] Prizearr = new  object[][]{  
        //Inside pointer rotation  
        {1, "First Prize", 0.0001},  
        {2, "Don't Lose Heart", 39.9994}, 
        { 3, "Good Luck", 20.0},  
        {4, "Second Prize", 0.0002},  
        {5, "persistent", 20.0},  
        {6, "Luck saved First", 10.0},  
        {7, "Third Prize", 0.0003},  
        {8, "Thank you for participating", 10.0} 
	};
Here we can add more parameters to the object, if your lottery needs an angle or a trophy position, you add the angle data (two values, one end of the start), and you also need to generate a random number based on the angle data.

The most important thing about the whole program is this lottery algorithm. Here we can control the level of prizes, quantity and so on, the specific need to add it, here only to achieve the lottery. Here is a small suggestion, in the introduction of the prize array time can be a large probability of the row in front, so the lottery will be a certain number of times the algorithm cycle reduced.

Get awards according to probability public static Integer Getrand (float obj[]) {Integer result = null; try {Float sum = total probability precision of 0.0f;//probability array float min = 0.0f;//for (int I=0;i<obj.leng
            	th;i++) {BigDecimal beforsum = new BigDecimal float.tostring (sum);
            	BigDecimal ObjValue = new BigDecimal (float.tostring (obj[i));
            sum = Beforsum.add (ObjValue). Floatvalue (); for (int i=0;i<obj.length;i++) {//probability array loop BigDecimal db = new BigDecimal (Math.random () *
            	um-min) + min);
                BigDecimal B = New BigDecimal (float.tostring (obj[i));  
                    if (CompareMethod (db,b) = = 1) {//Jackpot result = i;  
                Break  
                }else{sum-=obj[i];  
        A catch (Exception e) {e.printstacktrace ());
/** * Prize control Code, here you can control the level of our prizes * **/	    return result; }
The above is the turntable lottery algorithm. I hope it will be helpful to the necessary program members.

Related Article

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.