随便新建一个asp.net web项目,拖入即可。现在知道为什么游戏老是暴不出好装备,因为他的权重是非常小地。。。中午吃饭随机一下,想去哪一家,就把权重值设大一点,再中不了,就认命吧!
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>加权随机</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtNum" runat="server" Text="100" ></asp:TextBox> <br /> <br /> <asp:Button ID="btnRandom" runat="server" Text="开始抽奖(装备)" onclick="btnRandom_Click"/> <br /> <br /> <asp:Button ID="btnRandomFood" runat="server" Text="开始抽奖(餐厅)" onclick="btnRandomFood_Click"/> <br /> <br /> <asp:Button ID="btnClear" runat="server" Text="清屏" onclick="btnClear_Click"/> <br /> <asp:Literal ID="lblResult" runat="server"></asp:Literal> </div> </form> </body> </html>
Default.aspx.cs
"寒冰爪", 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 (KeyValuePair<string, int> kvp in Goods) { TotalWeight += kvp.Value; } } /// <summary> /// 初始化餐厅,公司楼下餐厅比较多,只写几个,意思意思。。。 /// </summary> protected void InitFood() { Goods.Clear(); TotalWeight = 0; Goods.Add("清真兰州", 1); Goods.Add("长安客", 1); Goods.Add("迎客松", 1); Goods.Add("吉祥混沌", 1); foreach (KeyValuePair<string, int> kvp 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 + "<br/>"; 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 (KeyValuePair<string, int> kvp 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 + " 暴出次数:" + c.ToString() + " 暴出率:" + rate + "%<br/>"; } } //开始抽奖(餐厅) protected void btnRandomFood_Click(object sender, EventArgs e) { InitFood(); lblResult.Text = lblResult.Text + "<br/>"; 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 (KeyValuePair<string, int> kvp 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 + " 暴出次数:" + c.ToString() + " 暴出率:" + rate + "%<br/>"; } } /// <summary> /// 根据随机权重判断所在范围 /// </summary> /// <param name="Weight"></param> protected void ProduceResult(int Weight) { int min = 1; int max = 1; foreach (KeyValuePair<string, int> kvp 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; } } /// <summary> /// 随机种子值(防止速度过快造成的重复) /// </summary> /// <returns></returns> 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 = ""; } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]