Some data structure ideas (3)

Source: Internet
Author: User
  • Design of Random Function Generator

Suppose you want to output 0 and 1 with a probability of 1/2 each. You can freely use a process biased-random that outputs 0 or 1. It outputs 1 with probability P and 0 with probability 1-P, where 0 <p <1, but you do not know the value of P. An algorithm using biased-random as a subroutine is provided to return a result without bias, that is, return 0 with a probability of 1/2, and return 1 with a probability of 1/2. What is the expected running time of your algorithm as a p function?

Algorithm analysis:

It is known that biased-random can generate 0 and 1, so 1-biased-random also produces 1 and 0, and outputs 1 at the probability of 1-P and 0 at the probability of P.

If we think of 1-biased-random as another function generator and combine it with biased-random to be called, we can conclude that:

Call result 00 01 10 11

Number of 1 0 1 1 2

Probability of occurrence (1-p) * (1-p) (1-p) * (1-p) p * (1-p)

The expected value of 1 for a call is: 0 * (1-p) * (1-p) + 1 * (1-p) * (1-p) + 1 * p * P + 2 * p * (1-p) = 1. If a pair is called four times, the expected number of 1 is four. Why do I need to call it four times? Because the probability of biased-random generating 0 is equal to that of 1-biased-random generating 1. The probability of biased-random generating 1 is equal to that of 1-biased-random generating 0, then, all the combined pairs (,) can be exactly overwritten for four times, and 8 or 16 times can be called. After four paired calls, count the number of occurrences of 1. If the number is less than four, 0 is returned. If the number is greater than four, 1 is returned (here it is equivalent to encapsulating four calls as a function ). But there is a problem. Should we return 0 or 1 if it is four times? (Because the number of possible times of 1 is 0 to 8), a large number of pairs can be called to make a single phenomenon negligible. For example, for 1024 calls, count the number of 1. If it is less than 1024, 0 is returned; otherwise, 1 is returned.

  • Random probability Generator

Question:

Known as a random generator, the probability of generating 0 is P, and the probability of generating 1 is 1-P. Now you need to construct a generator so that the probability of constructing 0 and 1 is 1/2.

Solution:

This is a typical topic of the random probability generator.

Because we need to generate 1/2, and one digit 0, or one digit 1 cannot generate an equal probability, we consider extending a random number to two digits:

00 p * P

01 P * (1-p)

10 (1-p) * P

11 (1-p) * (1-p)

According to the above analysis, 01 and 10 are equal probabilities, so we only need to generate 01 and 10.

As a result, we can discard 00 and 11, and only record 01 and 10. It can be set to "01" to "0" or "10" to "1". Then, equal probability 1/2 is used to generate 0 and 1.

Extension:

Known as a random generator, the probability of generating 0 is P, and the probability of generating 1 is 1-P. Now you need to construct a generator so that the probability of constructing 0 and 1 is 1/2; construct a generator so that the probability of constructing 1, 2, and 3 is 1/3 ;..., construct a generator to construct 1, 2, 3 ,... the probability of N is 1/N, which requires the lowest complexity.

Answer:

For n = 2, 01 indicates 0, 10 indicates 1, and other probabilities. In other cases, discard

For n = 3, 001 indicates 1, 010 indicates 2,100 indicates 3, and other probabilities.

For N = 4, think 0001 represents 1, 0010 represents 2, 0100 represents 3, represents 4, and other probability, other circumstances give up

First of all, in the case of 1/2, we generate two values at a time. If it is 00 or 11, it is discarded. Otherwise, the 01 value is 1, 10 is 0, and their probability is p * (1-p) is equal, so the probability is equal. Next, let's take the case of 1/n. Let's take 5 as an example. In this case, we take X = 2 because C (2x, x) = C) = 6 is the smallest x larger than 5. At this time, we will generate four binary values at a time, and discard all the values where the number of 1 is not 2. At this time, there will be six remaining values, take the minimum five, that is, discard 1100. Then, for the first five numbers 1 to 5, their probabilities are all P * p * (1-p) * (1-p) equal.

The key is to find the smallest X, so that C (2x, x)> = n can improve the search efficiency.

  • How many random numbers (0, 1) must be obtained on average to make and exceed 1

The most fascinating thing about mathematical constants is that they often appear in seemingly unrelated places. Take a random number between 0 and 1, add another random number between 0 and 1, and then add a random number between 0 and 1 ?? Until and beyond 1. An interesting question: How many times does it take to increase the sum to over 1? The answer is E.

#define NUM 9999999     int main()  {     int sum=0;  srand(time(NULL));  for (int i=0;i<NUM;i++)      {      double val=0;      while(val <1)      {          val+=(rand()/(double)RAND_MAX);          sum++;      }      }  printf("%f\n",sum/(double)NUM);  return 0;  }

To prove this, let's first look at a simpler question: How likely is the sum of the two real numbers between 0 and 1? It is easy to think that the point (x, y) Satisfying X + Y <1 occupies half of the Square (0, 1) x (0, 1, therefore, the probability that the sum of the two real numbers is less than 1 is 1/2. Similarly, the probability of the sum of three numbers less than 1 is 1/6, which is a cubic pyramid captured in the Unit Cube by plane x + y + z = 1. This 1/6 can be obtained through simple integral points by using the similarity relationship between the cross section and the bottom surface:

The probability that the sum of the four random numbers between 0 and 1 is less than 1 is equal to the "volume" in the corner of the four-dimensional cube. Its "bottom" is a three-dimensional body with a volume of 1/6, on the fourth dimension, you can obtain its "volume" by adding points"

Round (0 .. 1) (x ^ 3) * 1/6 dx = 1/24

So far, the probability that the sum of n random numbers does not exceed 1 is 1/n! In turn, the probability that the sum of N numbers is greater than 1 is 1-1/n! So the probability that the number of n just exceeds 1 is

(1-1/N !) -(1-1/(n-1 )!) = (N-1)/n!

Therefore, to make the sum greater than 1, we need to accumulate the expected number of times

Σ (n = 2 .. ∞) N * (n-1)/n! = Σ (n = 1 .. ∞) n/n! = E

  • Meaning of X & (x-1) Expressions

Evaluate the return value of the following function (Microsoft) -- count the number of 1

int func(int x){    int countx = 0;    while(x)    {        countx++;        x = x&(x-1);    }    return countx;}

Assume x = 9999

10011100001111

Answer: 8

Train of Thought: Convert X to a binary system and check the number of contained 1.

Note:Every time x = x & (x-1) is executed, a 1 on the rightmost side of X is changed to 0 when X is represented in binary, because the X-1 will change this bit (A 1 on the rightmost when X is represented in binary) to 0.

 

Determines whether a number (x) is the N power of 2.

#include <stdio.h>int func(int x){    if( (x&(x-1)) == 0 )        return 1;    else        return 0;}int main(){    int x = 8;    printf("%d\n", func(x));}

Note:

(1)If a number isThe N power of 2, then when this number is expressed in binary, its highest bit is 1, and the remaining bit is 0.

(2)= The priority is higher &

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.