2015 Ali Internship Line PEN Question-additional question 1

Source: Internet
Author: User
Tags natural logarithm

Random number generator

The random number generator used by the computer is often pseudo-random, in order to achieve the statistical sense of true random number, you can need to introduce system
As random seeds (such as the Entropy Pool in Unix systems). Suppose one day there was the coin function of God: int G ();
Because the god coins used here may not be uniform. However, it is guaranteed that g () can return the probability of X-1,1-x to 0, where x is an unknown constant (and x is not equal to 0 or 1).

Please implement the objective function: int F (double p);
Requirements

      1. The F function returns 1 with probability p and returns 0 with 1-p.
      1. Any library functions that are not used except for G. PS: Define Macro UINT_MAX=0XFFFFFFFF

Based on the above-mentioned similar idea, please construct the function to find the approximate value of the following irrational numbers:

      1. Double Pi (); Pi Pi
      1. Double E (); The base e of the natural logarithm function.

Tip: As a simulation process, the maximum number of repetitions can be introduced, please describe the idea and complete the code.

Ideas:

    • Generated by the G () equal probability random number generation function, can refer to the introduction of the algorithm or this blog
    • The number of times ( for example, 10 times ) is random by an equal-to-random function, and 1024 results are generated, and each structure is equal. Then we can analyze each random result, each random result can be expressed in decimal, for example:
0 1 0 1 0 0 1 0 1 1

Can be expressed in decimal 331, all possible decimal in the [0, 1024) range. So as long as the generated decimal number is less than 1024*p returns 1, greater than the return 0, so that the requirements can be met.

#include <stdlib.h>#include <iostream>using namespace STD;intFDoubleP) {intsum =0;intt =Ten;intall =1024x768;intT1 = all * p; for(inti =0; I < T; ++i) sum + = U () <<i;if(Sum < T1)return 1;Else if(Sum >= t1)return 0;}//Generate the uniform random function//reference book: <<algorithm introduction>>intU () {intN1 =-1, N2 =-1; Do{n1 = G (); N2 = G ();if(N1 = =0&& N2 = =1)return 1;Else if(N1 =1&& N2 = =0)return 0; } while(N1 = =0&& N2 = =0) || (N1 = =1&& N2 = =1))Exit(-1);}

2015 Ali Internship Line PEN Question-additional question 1

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.