This article mainly introduces. net makes small programs for restaurants and game equipment, and friends will no longer have to consider where to eat every day. it is very fun and practical, for more information, see this article. net makes small programs for restaurants and game equipment. friends no longer need to consider where to eat every day. it is very fun and practical. if you need it, please refer to it.
Create an asp.net web project and drag it in. Now I know why the game is always vulnerable because its weight is very small... Eat at random at noon. if you want to go to any one, set the weight value to a greater value. if you can't get it, just quit!
Default. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> Weighted random
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 {///
/// Name ///Public string Name {get; set ;}///
/// Weight (greater than or equal to 1; otherwise, the probability of occurrence is 0 )///Public int Weight {get; set ;}} public List
Result = new List
(); Protected void Page_Load (object sender, EventArgs e ){}///
/// Initialize the device. you are the player of "Dream Three Kingdoms ///Protected void InitGoods () {Goods. clear (); TotalWeight = 0; Goods. add ("cold ice claw", 2); // artifact (fist claw) Goods. add ("weight fur", 300); Goods. add ("small fur", 1000); Goods. add ("light fur", 1000); Goods. add ("create a book with defense tools of God", 5); Goods. add ("Legend weapon creation Book", 20); Goods. add ("Rosewood branches", 300); Goods. add ("hard fur", 600); Goods. add ("hook soul Crystal", 8); Goods. add ("Zhenlong zhiwujian", 30); Goods. add ("Dong Zhuo hook soul", 1); // The artifact (long-handled) foreach (KeyValuePair
Kvp in Goods) {TotalWeight + = kvp. Value ;}}///
/// Initialize the restaurant. There are many restaurants downstairs in the company, which only contain a few... ///Protected void InitFood () {Goods. clear (); TotalWeight = 0; Goods. add ("halal Lanzhou", 1); Goods. add ("Changan Ke", 1); Goods. add ("welcome pine", 1); Goods. add ("auspicious chaos", 1); foreach (KeyValuePair
Kvp in Goods) {TotalWeight + = kvp. value ;}} protected int GetTryParse () {try {return int. parse (txtNum. text);} catch {return 1 ;}// start drawing (equipped with) 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 + "item name:" + kvp. key + "weight:" + kvp. value + "Times exposed:" + c. toString () + "outbreak rate:" + rate + "%
";}}// Start drawing (restaurant) 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 + "restaurant name:" + kvp. key + "weight:" + kvp. value + "Times exposed:" + c. toString () + "outbreak rate:" + rate + "%
";}}///
/// Determine the range based on the random weight //////
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 ;}}///
/// Random seed value (to prevent duplication caused by excessive speed )//////
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);} // clear screen operation protected void btnClear_Click (object sender, EventArgs e) {lblResult. text = "";}}
The above is all the content of this article. I hope you will like it.
The above is to share the details of the small programs for restaurants and game equipment produced by. net. For more information, please refer to other related articles in the first PHP community!