怎么用.net制作抽奖程序-创新互联
这篇文章主要介绍“怎么用.net制作抽奖程序”,在日常操作中,相信很多人在怎么用.net制作抽奖程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用.net制作抽奖程序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!


Default.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,否则出现的可能性为0)
///
public int Weight
{
get;
set;
}
}
public List Result = 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 (KeyValuePair kvp 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 (KeyValuePair 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 + "
";
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 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 + "%
";
}
}
//开始抽奖(餐厅)
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 (KeyValuePair 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 + "%
";
}
}
///
/// 根据随机权重判断所在范围
///
///
protected void ProduceResult(int Weight)
{
int min = 1;
int max = 1;
foreach (KeyValuePair 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;
}
}
///
/// 随机种子值(防止速度过快造成的重复)
///
///
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制作抽奖程序”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
名称栏目:怎么用.net制作抽奖程序-创新互联
链接URL:http://www.jxjierui.cn/article/jpcod.html


咨询
建站咨询
