双色球根据历史概率选球

根据历史数据编写了一个双色球选球策略,策略其实很简单。

1.统计1-33号红球以及1-16号篮球每个球出现的次数。

2.计算所有球出现的总次数,用总次数分别去除每个号出现的次数,得到球号对应的权值,同时,计算出总权值。再用球号权值/总权值得到球号对应的概率。

3.遍历所有球号,随机出一个0到1的小数,判断小数落入球号概率累积的区间内,即完成一次抽号。

多说无益,直接上代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class lecai {

	public static float total = 0.0f;
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		File folder = new File("C:\\Users\\Administrator\\Desktop\\历史彩票");
		File[] files = folder.listFiles();
		Map<Integer,Integer>  redball= new HashMap<Integer,Integer>();
		Map<Integer,Integer>  blueball= new HashMap<Integer,Integer>();
		for(File file:files) {
			FileInputStream fis = new FileInputStream(file);
			
			BufferedReader fr = new BufferedReader(new InputStreamReader(fis));
			
			String line = "";
			
			while((line = fr.readLine())!=null)
			{
				line = line.substring(11,31);
				//System.out.println(line);
				int blueballnum = Integer.parseInt(line.substring(18));
				//System.out.println(blueballnum);
				if(blueball.containsKey(blueballnum)){
					blueball.put(blueballnum, blueball.get(blueballnum)+1);
				}else
				{
					blueball.put(blueballnum, 1);
				}
				
				String redballline = line.substring(0,17);
				
				String[] redballnums = redballline.split(",");
				
				for(String redballnumstr : redballnums)
				{
					int redballnum = Integer.parseInt(redballnumstr);
					if(redball.containsKey(redballnum)){
						redball.put(redballnum, redball.get(redballnum)+1);
					}else
					{
						redball.put(redballnum, 1);
					}
				}
			}
		}
		List<Integer> blueballs = calculate(blueball, 1);
		List<Integer> redballs = calculate(redball, 6);
		System.out.println("随机蓝球号码:"+blueballs.toString()+"   随机红球号码:"+redballs.toString());
	}
	
	private static List<Integer> calculate(Map<Integer,Integer> ball, int num)
	{
		  System.out.println("------------------------------------------------计算概率开始--------------------------------------------------");
		  total = 0.0f;
		  List<keyvalue> ballobejct = new ArrayList<keyvalue>();
		  int redtotal=0;
		  for (Map.Entry<Integer, Integer> entry : ball.entrySet()) {
			  //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			  redtotal += entry.getValue();
		  }
		  
		  //球便利
		  for (Map.Entry<Integer, Integer> entry : ball.entrySet()) {
			  ballobejct.add(new keyvalue(((float)redtotal/(float)entry.getValue()),entry.getValue(),entry.getKey()));
		  }
		  
		  //输出总数
		  System.out.println(total);
		  
		  //计算概率
		  for(keyvalue kv : ballobejct) {
			  kv.setPercent(kv.getKey()/total);
		  }
		  
		  Collections.sort(ballobejct);
		  
		  for(keyvalue kv : ballobejct) {
			  System.out.println(kv.toString());
		  }
		  
		  List<Integer> balls = new ArrayList<Integer>();
		  double totalpercent;
		  for(int i = 0; i<num; i++){
			  totalpercent = 0.0;
			  //System.out.println(Math.random());
			  double ran = Math.random();
			  for(keyvalue kv : ballobejct)
			  {
				  if(ran>=totalpercent && ran<(totalpercent+kv.getPercent())){
					  /**
					   *  这个概率已经随机过
					   */
					  if(balls.contains(kv.getNum())){
						  i--;
						  break;
					  }
					  balls.add(kv.getNum());
					  break;
				  }
				  totalpercent+=kv.getPercent();
			  }
		  }
		  System.out.println("------------------------------------------------计算概率结束--------------------------------------------------");
		  return balls;
		  
	}
}
public class keyvalue implements Comparable<keyvalue>{
	/*
	 * 总次数/该号码出现的次数
	 */
	private float key;
	/*
	 * 号码
	 */
	private int num;
	/*
	 * 该号彩票概率
	 */
	private float percent;
	/*
	 * 该彩票出现的次数
	 */
	private int count; 
	public keyvalue(float key,int count,int num) {
		super();
		this.key = key;
		this.num = num;
		this.count = count;
		lecai.total += key;
	}

	public float getKey() {
		return key;
	}

	public void setKey(float key) {
		this.key = key;
	}
	
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public float getPercent() {
		return percent;
	}

	public void setPercent(float percent) {
		this.percent = percent;
	}
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public int compareTo(keyvalue other) {
		// TODO Auto-generated method stub
		return this.getKey()>=other.getKey()?1:-1;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return  "key= " + this.getKey() + " and num= " + this.getNum()+" and count= " + this.getCount()+" and percent= " + this.getPercent();
	}
}

资源地址:

http://download.csdn.net/detail/wjw905266/9012109

猜你喜欢

转载自betakoli.iteye.com/blog/2235805