Implementation of probability events

Source: Internet
Author: User

A few days ago, when fixing the bug of the game equipment enhancement function, I found that the former colleagues in charge of this function used a bad way to achieve the enhancement success rate of different levels of equipment, so I overwrote his code.

The implementation method is roughly divided into the following steps:
1. Obtain the enhancement success rate and failure rate (a percentage, for example, the success rate of 20%, equals to the value 20) of the specified equipment)
2. Create a set a for step 3, step 4, or step 5.
3. loop from 0 to (success rate * 10), insert a string 'success 'To A (assuming the success rate is 20%, this set has 200 'success' strings)
4. loop from 0 to (failure rate * 10), insert a 'failed' string to A (assuming the failure rate is 80%, this set has 800 'failed' strings)
5. Select 0 ~ Random Number I between 999, take a [I], judge whether it is 'success' or 'failed', and then perform the corresponding operation

It is a very tedious and inefficient algorithm, that is, a waste of CPU time and memory space, but it does not get any benefits. It also makes the code lengthy and difficult to read.

In fact, to refactor the above logic, I only used a line of code:
0 ~ Random Number between 9999 I, I

To increase the random number randomness, I increased the random number range by 10 times.

The following is the test code. Through 100 groups, each group generates a random number 100 times and then obtains the average value, it can be proved that the algorithm described above only needs one line of code, but it is very reliable.

<?php$rate = 20;$t1 = 0;for ($j = 0; $j < 100; $j ++){    $t = 0;        for ($i = 0; $i < 100; $i++)    {        if(rand(0, 9999) <= $rate * 100)        {            $t += 1;        }    }        $t1 += $t;}echo $t1 / 100;?>

I thought this was a basic thing. I could only say that my colleague was not good enough or had insufficient sense of responsibility to write bad code, so I didn't want to write this article.
But considering that there is no one to share, there may still be some less experienced programmer friends who will adopt the same disgusting or more disgusting implementation methods at work, so I wrote it.

In fact, there are many things worth pondering, not just the simplicity of program algorithms.

I often say that the occurrence of a single thing is not the result of a single force, but the result of a combination of multiple forces.
In this case, we should not only see the lack of developer capabilities, but also be aware of the negligence of project managers in the supervision process and whether the design seminar should be introduced in the development process.

A few hundred words are omitted here and sent to Weibo. This article ends here.

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.