• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    .net制作的抽餐厅和游戏装备的小程序

    随便新建一个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

    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
    {
      //string,int>:名称:权重>
      public Dictionarystring, int> Goods = new Dictionarystring, int>();
      public int TotalWeight = 0;
     
      public class Good
      {
        /// summary>
        /// 名称
        /// /summary>
        public string Name
        {
          get;
          set;
        }
        /// summary>
        /// 权重(大于等于1,否则出现的可能性为0)
        /// /summary>
        public int Weight
        {
          get;
          set;
        }
      }
     
      public ListGood> Result = new ListGood>();
     
      protected void Page_Load(object sender, EventArgs e)
      {
      }
     
      /// summary>
      /// 初始化装备 俺是《梦三国》玩家,嘿嘿
      /// /summary>
      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 (KeyValuePairstring, 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 (KeyValuePairstring, 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 (KeyValuePairstring, 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 + "nbsp;权重:" + kvp.Value + "nbsp;暴出次数:" + c.ToString() + "nbsp;暴出率:" + 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 (KeyValuePairstring, 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 + "nbsp;权重:" + kvp.Value + "nbsp;暴出次数:" + c.ToString() + "nbsp;暴出率:" + rate + "%br/>";
        }
     
      }
     
      /// summary>
      /// 根据随机权重判断所在范围
      /// /summary>
      /// param name="Weight">/param>
      protected void ProduceResult(int Weight)
      {
        int min = 1;
        int max = 1;
        foreach (KeyValuePairstring, 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 = "";
      }
    }
    

    以上所述就是本文的全部内容了,希望大家能够喜欢。

    您可能感兴趣的文章:
    • .NET实现魔方游戏(一)之任意阶魔方的表示
    • .NET开发实现一个微信跳一跳的辅助程序
    上一篇:Ajax+asp.net实现用户登陆
    下一篇:如何合并多个 .NET 程序集
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    .net制作的抽餐厅和游戏装备的小程序 .net,制作,的,抽,餐厅,和,