這篇文章主要介紹“怎么用.net制作抽獎程序”,在日常操作中,相信很多人在怎么用.net制作抽獎程序問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用.net制作抽獎程序”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計制作、做網(wǎng)站與策劃設計,船營網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:船營等地區(qū)。船營做網(wǎng)站價格咨詢:18982081108Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>加權隨機
Default.aspx.cs
?using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { //:<名稱:權重> public Dictionary Goods = new Dictionary (); public int TotalWeight = 0; public class Good { /// /// 名稱 /// public string Name { get; set; } ////// 權重(大于等于1,否則出現(xiàn)的可能性為0) /// public int Weight { get; set; } } public ListResult = new List (); protected void Page_Load(object sender, EventArgs e) { } /// /// 初始化裝備 俺是《夢三國》玩家,嘿嘿 /// protected void InitGoods() { Goods.Clear(); TotalWeight = 0; Goods.Add("寒冰爪", 2); //神器(拳爪類) Goods.Add("重毛皮", 300); Goods.Add("小毛皮", 1000); Goods.Add("輕毛皮", 1000); Goods.Add("神之防具打造書", 5); Goods.Add("傳說武器打造書", 20); Goods.Add("紫檀樹枝", 300); Goods.Add("硬毛皮", 600); Goods.Add("勾魂水晶", 8); Goods.Add("真龍炙舞劍", 30); Goods.Add("董卓勾魂", 1); //神器中的神器(長柄類) foreach (KeyValuePairkvp in Goods) { TotalWeight += kvp.Value; } } /// /// 初始化餐廳,公司樓下餐廳比較多,只寫幾個,意思意思。。。 /// protected void InitFood() { Goods.Clear(); TotalWeight = 0; Goods.Add("清真蘭州", 1); Goods.Add("長安客", 1); Goods.Add("迎客松", 1); Goods.Add("吉祥混沌", 1); foreach (KeyValuePairkvp in Goods) { TotalWeight += kvp.Value; } } protected int GetTryParse() { try { return int.Parse(txtNum.Text); } catch { return 1; } } //開始抽獎(裝備) protected void btnRandom_Click(object sender, EventArgs e) { InitGoods(); lblResult.Text = lblResult.Text + "
"; int Count = GetTryParse(); for (int i = 1; i <= Count; i++) { Random rdm = new Random(GetRandomSeed()); int Weight = rdm.Next(1, TotalWeight + 1); ProduceResult(Weight); } foreach (KeyValuePairkvp in Goods) { int c = Result.Count(d => d.Name == kvp.Key); double rate = c * 1.0 / Count * 1.0 * 100; lblResult.Text = lblResult.Text + "物品名稱:" + kvp.Key + " 權重:" + kvp.Value + " 暴出次數(shù):" + c.ToString() + " 暴出率:" + rate + "%
"; } } //開始抽獎(餐廳) protected void btnRandomFood_Click(object sender, EventArgs e) { InitFood(); lblResult.Text = lblResult.Text + "
"; int Count = GetTryParse(); for (int i = 1; i <= Count; i++) { Random rdm = new Random(GetRandomSeed()); int Weight = rdm.Next(1, TotalWeight + 1); ProduceResult(Weight); } foreach (KeyValuePairkvp in Goods) { int c = Result.Count(d => d.Name == kvp.Key); double rate = c * 1.0 / Count * 1.0 * 100; lblResult.Text = lblResult.Text + "餐廳名稱:" + kvp.Key + " 權重:" + kvp.Value + " 暴出次數(shù):" + c.ToString() + " 暴出率:" + rate + "%
"; } } ////// 根據(jù)隨機權重判斷所在范圍 /// /// protected void ProduceResult(int Weight) { int min = 1; int max = 1; foreach (KeyValuePairkvp in Goods) { max = min + kvp.Value - 1; if (Weight >= min && Weight <= max) { Good g = new Good(); g.Name = kvp.Key; g.Weight = kvp.Value; Result.Add(g); return; } min = max + 1; } } /// /// 隨機種子值(防止速度過快造成的重復) /// ///private static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); } //清屏操作 protected void btnClear_Click(object sender, EventArgs e) { lblResult.Text = ""; } }
到此,關于“怎么用.net制作抽獎程序”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前名稱:怎么用.net制作抽獎程序-創(chuàng)新互聯(lián)
新聞來源:http://weahome.cn/article/jpcod.html