In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve the red packet snatching algorithm in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Double mean value method (fair version)
What rules need to be met to issue a red packet with a fixed amount and be robbed by a number of individuals?
1. The sum of the amount snatched by everyone is equal to the amount of red packet, which cannot be exceeded or less.
two。 Everyone gets at least a penny.
3. Make sure that everyone has an equal chance of getting the amount.
Assuming that the amount of the remaining red packet is M and the remaining number of people is N, then there is the following formula:
The amount of each grab = random interval (0, M / N × 2)
This formula ensures that the average value of each random amount is equal and will not be unfair because of the sequence of grabbing red packets. For example:
Suppose there are 10 people with a total red packet of 100 yuan. 100 / 10 × 2 = 20, so the random range of the first person is (0Power20), and you can get 10 yuan on average.
Suppose the first person is randomly assigned to 10 yuan, then the remaining amount is 100-10 = 90 yuan. 90 / 9 × 2 = 20, so the random range of the second person is the same (0Preline 20), and you can get 10 yuan on average.
Suppose the second person is random to 10 yuan, then the remaining amount is 90-10 = 80 yuan. 80 / 8 × 2 = 20, so the random range of the third person is the same (0Preline 20), and you can get 10 yuan on average.
And so on, the mean of each random range is equal.
Static void Main (string [] args) {for (int I = 0; I < 10; iTunes +) {var list = DivideRedPackage (100* 100,10); Console.WriteLine (string.Join (",", list)); int count = 0 Foreach (var item in list) {count + = item;} Console.WriteLine (count);} System.Console.ReadKey () } / generate red packet array / total red packet amount, unit points / number of red packets / static List DivideRedPackage (int cashCount, int peopleNumber) {List redPackageList = new List (); if (cashCount)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.