. Net is a small program for restaurants and game equipment, and. net Game equipment.

Source: Internet
Author: User
Tags random seed

. Net is a small program for restaurants and game equipment, and. net Game equipment.

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" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

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 >:< name: weight> public Dictionary <string, int> Goods = new Dictionary <string, int> (); public int TotalWeight = 0; public class Good {// <summary> /// Name // </summary> public string Name {get; set ;}/// <Summary> // Weight (greater than or equal to 1; otherwise, the probability of occurrence is 0) /// </summary> public int Weight {get; set ;}} public List <Good> Result = new List <Good> (); protected void Page_Load (object sender, EventArgs e) {}/// <summary> /// initialize the device. You are a player in the "Dream Three Kingdoms". Hey, /// </summary> 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 <string, int> kvp in Goods) {TotalWeight + = kvp. value ;}}/// <summary> /// initialize the restaurant. There are many restaurants downstairs in the company, which only contain a few words... /// </Summary> 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 <string, int> 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 + "<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 + "item name:" + kvp. key + "Weight:" + kvp. value + "times exposed:" + c. toString () + "outbreak rate:" + rate + "% <br/>" ;}}// start drawing (Restaurant) 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 + "restaurant name:" + kvp. key + "Weight:" + kvp. value + "times exposed:" + c. toString () + "outbreak rate:" + rate + "% <br/> ";}} /// <summary> /// determine the range based on the random Weight /// </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> // Random Seed value (to prevent duplication due to excessive speed) /// </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);} // 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.