- 最後登錄
- 2024-4-18
- 在線時間
- 0 小時
- 註冊時間
- 2011-8-12
- 閱讀權限
- 20
- 精華
- 0
- UID
- 10169090
- 帖子
- 87
- 積分
- 217 點
- 潛水值
- 18862 米
| 所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。 本帖最後由 cjoushua 於 2013-12-27 07:57 AM 編輯
題目:大樂透預測軟體
規則一:上一期出現過的號碼不再出現
規則二:依照歷史的統計機率去計算數字出現機率
這是小弟練習所寫的小範例,如果有更好的方法或是寫法請多多批評跟指教
第一期:
第二期:
草創版本規則尚待持續修正
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace Lotto
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //By Joushua 20131226
- //機率陣列(歷年以來每個數字出現的機率)
- double[] probability = new double[] { 0.023947284, 0.023625844, 0.020411443, 0.020411443, 0.021215043, 0.018643523, 0.020090003, 0.022179364, 0.017357763, 0.018482803, 0.021536483, 0.019929283, 0.020090003, 0.018000643, 0.022179364, 0.020250723, 0.016393443, 0.018964963, 0.018000643, 0.019768563, 0.020250723, 0.022822244, 0.023143684, 0.018322083, 0.020572163, 0.021215043, 0.020090003, 0.023143684, 0.018000643, 0.021215043, 0.022982964, 0.018482803, 0.017679203, 0.019447123, 0.020090003, 0.022982964, 0.019286403, 0.021857923, 0.021536483, 0.018964963, 0.019286403, 0.020893603, 0.023304404, 0.020090003, 0.021375763, 0.020732883, 0.019607843, 0.020732883, 0.020411443 };
- //取樣陣列
- int[] sample = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 };
- //rndold上一次的預測
- ArrayList rndold = new ArrayList();
- rndold.Add(Convert.ToInt32(label1.Text));
- rndold.Add(Convert.ToInt32(label2.Text));
- rndold.Add(Convert.ToInt32(label3.Text));
- rndold.Add(Convert.ToInt32(label4.Text));
- rndold.Add(Convert.ToInt32(label5.Text));
- rndold.Add(Convert.ToInt32(label6.Text));
- rndold.Add(Convert.ToInt32(label7.Text));
- ArrayList rnd = MakeRand(sample,probability, 7, rndold);
- rndold = rnd;
-
- label1.Text = zero(rnd[0].ToString());
- label2.Text = zero(rnd[1].ToString());
- label3.Text = zero(rnd[2].ToString());
- label4.Text = zero(rnd[3].ToString());
- label5.Text = zero(rnd[4].ToString());
- label6.Text = zero(rnd[5].ToString());
- //特別號
- label13.Text = zero(rnd[6].ToString());
- int[] arr = new int[] { Convert.ToInt32(label1.Text), Convert.ToInt32(label2.Text), Convert.ToInt32(label3.Text), Convert.ToInt32(label4.Text), Convert.ToInt32(label5.Text), Convert.ToInt32(label6.Text) };
- sortm(arr);
- label7.Text = zero(arr[0].ToString());
- label8.Text = zero(arr[1].ToString());
- label9.Text = zero(arr[2].ToString());
- label10.Text = zero(arr[3].ToString());
- label11.Text = zero(arr[4].ToString());
- label12.Text = zero(arr[5].ToString());
- }
- //產生不重複的亂數
- //sample 傳入陣列
- //probability 陣列的機率
- //intNum 產生亂數的數量
- //rndold 上次的預測
- private ArrayList MakeRand(int[] sample, double[] probability, int intNum,ArrayList rndold)
- {
- ArrayList arrayRand = new ArrayList();
- //Random rnd = new Random((int)DateTime.Now.Ticks);
- Random rnd = new Random();
- int intRnd;
- while (arrayRand.Count < intNum)
- {
- intRnd = GetChance(rnd, sample, probability);
- //所產出的值不能在arrayRand內
- if (!arrayRand.Contains(intRnd))
- {
- //所產出的值不能在rndold內
- if (!rndold.Contains(intRnd))
- {
- arrayRand.Add(intRnd);
- }
- }
- }
- return arrayRand;
- }
- //個位數捕0
- public string zero(string z)
- {
- if (z.Length < 2)
- {
- z = "0" + z;
- }
- return z;
- }
- //從小排到大
- public void sortm(int[] arr)
- {
- int arrbuff;
- for (int i = 0; i <= arr.Length - 1; i++)
- {
- for (int j = i + 1; j < arr.Length; j++)
- {
- if (arr[i] > arr[j])
- {
- arrbuff = arr[i];
- arr[i] = arr[j];
- arr[j] = arrbuff;
- }
- }
- }
- return;
- }
- //取得抽樣結果(帶入參數: 亂數物件, 取樣陣列, 機率陣列)
- static int GetChance(Random rnd, int[] sample, double[] probability)
- {
- //機率陣列總和必須等於100
- //if (probability.Sum() != 100) return -2;
- //取樣陣列count必須等於機率陣列count
- if (sample.Length != probability.Length) return -3;
- double rndProbability = rnd.NextDouble();//傳回 0.0 和 1.0 之間的亂數。
- double totalProbability = 0;
- int itemIndex = 0;
- foreach (double p in probability)
- {
- //0=0+機率陣列內的probability[p]
- totalProbability += p;
- //totalProbability = totalProbability + probability[p]直到超過亂數的值
- if (totalProbability >= rndProbability )
- //回傳取樣陣列的結果值
- return sample[itemIndex];
- //0=0+1
- itemIndex++;
- }
- return -1;
- }
- }
- }
複製代碼
... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |