# [Random number] online gaming mat equipment and thoughts

Source: Internet
Author: User

Students with no skills are more stupid than those without any skills ---- Franklin

When you play games, you will always have a lot of experiences. When you perform a slight search on the Internet, you will find a lot of comments about pad equipment. Many people believe that pad equipment is useful. This is a problem !!

OK. Let's simplify the problem. Because the probability of building and merging equipment is complex, it is necessary to simplify the problem. Q:

When you throw a coin consecutively, what is the probability of failure N + 1 after N times of failure? Will it be higher than 50%(!!! This is a problem we really need to solve ).

Coin throwing is a random event. theoretically, the probability of a successful attempt is 50% (the portrait is facing up or not), and there is no relation between the two attempts. otherwise, it will not be called a random event. I was not good at probability theory, but I still had some consciousness in my mind. I don't believe that continuous failure can significantly increase the probability of success!

However, the theory is too poor. I cannot prove that the probability of N + 1 is still so high ..... well, I can only write code to see what the simulated real situation looks like:

`#include <stdlib.h>#include <time.h>#include <iostream>#include <Windows.h>#include <assert.h>#pragma comment(lib, "advapi32.lib")#define RANDOM_TIMES1000000#define FAIL_TIMES5#define FAIL_PERCENT50#define PERCENT_MAX100//#define C_RANDOM#ifdef C_RANDOM//nop#else#define RAND_MAX65535static HCRYPTPROVhProvider = 0;static const DWORDdwLength = 2;static BYTEpbBuffer[dwLength] = {};#endifstatic void random_init(){#ifdef C_RANDOMsrand((int)time(0));#elseDWORD result =::CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);assert(result);#endif}static void random_close(){#ifdef C_RANDOM//nop#else::CryptReleaseContext(hProvider, 0);#endif}static int _random(){unsigned short _rand_value = 0;#ifdef C_RANDOM_rand_value = (unsigned short)rand();#else DWORD result = ::CryptGenRandom(hProvider, dwLength, pbBuffer);assert(result);_rand_value = *(unsigned short*)pbBuffer;#endifreturn _rand_value;}static long random_count = 0;static int random_result(){int times = 0;while(times < FAIL_TIMES){int _num = _random();random_count++;if((1.0f*_num/RAND_MAX) > (1.0f*FAIL_PERCENT/PERCENT_MAX)){times++;}else{times = 0;}}random_count++;return _random();}int main(int argc, char* argv[]){random_init();long times_total = 0;long times_fail = 0;for(int times = 0; times < RANDOM_TIMES;++times){int x = random_result();if((1.0f*x/RAND_MAX) > (1.0f*FAIL_PERCENT/PERCENT_MAX))times_fail++;times_total++;}std::cout<<"total: "<<times_total<<std::endl;std::cout<<"fail: "<<times_fail<<std::endl;std::cout<<"random_count: "<<random_count<<std::endl;system("pause");random_close();return 0;}`

Two random numbers are used here. One is the standard C random number, and the other is CryptGenRandom. in Windows, there is no/dev/random and/dev/urandom, so which API is used instead.

I don't want to explain the code too much. The more important ones are the macros. I have nothing to do with changing the macros and running them. Let's take a look at the results:-) the two results of CryptGenRandom on my side:

`Total: 1000000 fail: 499497random_count: 62905788 press any key to continue... total: 1000000 fail: 499914random_count: 62979706 press any key to continue...`

In addition, the running result of the Standard C random number is as follows:

`Total: 1000000 fail: 500330random_count: 63103246 press any key to continue...`

We can see that the simulation results are similar several times:After N consecutive failures, the probability of N + 1 times remains unchanged.This is called a random event .:-)

But the problem is not over yet. here we need to set up a random number with a very good quality and there is no association between two random numbers. in fact, the random number of standard C is difficult to achieve this (pseudo-random number generator ). pseudo-random number, which may be cracked and predicted. The real random number will not:-D.

Therefore, when online games perform random queries related to RMB, you can consider real random numbers or RNG with a better quality, libc rand/rand_r is enough ~~~~

PS:

I remember a plan. The probability of dropping a monster was set to be too low (20% or 25%), and then it was said that there were more than 40 monsters, and none of them fell .... later it was replaced by rand_r, and the effect was much better.

Refer:

Http://msdn.microsoft.com/en-us/library/aa379942 (v = vs.85). aspx

Related Keywords:
Related Article

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.