67. Summary: random Number & quot; equi probability & quot; vs & quot; unequal probability & quot; generate related questions [random generator with equal or unequal probability]

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/random-generator-with-equal-or-unequal-probability.html

1. equi probability generation (1) rand5 generate rand3

There is now a Rand5 function that can generate random integers in the range of [0, 5) with equal probability. This function is required to write a Rand3 function (In addition, can no longer use any function or data source that can generate random numbers), generate random integers in the range of [0, 3) equal probability.

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
// Use rand5 to generate rand3
Int Rand3 ()
{
Int x;
Do
{
X = Rand5 ();
}
While (x> = 3 );
Return x;
}
(2) rand3 generate rand5 C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
// Use rand3 to generate rand5
Int Rand5 ()
{
Int x;
Do
{
X = Rand3 () * 3 + Rand3 ();
}
While (x> = 5 );
Return x;
}
(3) rand5 generate rand7 C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
// Use rand5 to generate rand7
Int Rand7 ()
{
Int x;
Do
{
X = Rand5 () * 5 + Rand5 ();
}
While (x> = 21 );
Return x % 7;
}
(4) rand7 generate rand10 C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
// Use rand7 to generate rand10
Int Rand10 ()
{
Int x;
Do
{
X = Rand7 () * 7 + Rand7 ();
}
While (x> = 40 );
Return x % 10;
}
(5) rand_m generates rand_n

Summary: this problem is further abstracted. It is known that the range of the random_m () random number generator is [0, m), and random_n () is used to generate a function in the range of [0, n, m <n & n <= m * m.

Set t to the maximum multiple of n and meet t <m * m, that is, t = (m * m)/n) * n;

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
// Use rand_m to generate rand_n
Int Rand_n ()
{
Int x;
Do
{
T = (m * m)/n) * n;
X = Rand_m () * m + Rand_m ();
}
While (x> = t );
Return x % n;
}
2. Unequal probability generation (1) how to generate a random number with the following probability? 0 appears once, 1 appears twice, 2 Appears 3 times, n-1 appears n times? C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
Int random (int size)
{
While (1)
{
Int m = rand (size );
Int n = rand (size );
If (m + n <size)
Return m + n;
}
}
(2) rand generates 01 with an unequal probability. How to generate 1 ~ with an equal probability of 1/n ~ Any number between n?

We know that the random function rand () generates 0 with the probability of p and 1 with the probability of 1-p. Now we need to design a new random function newRand (), make it generate 1 ~ with an equal probability of 1/n ~ Any number between n.

Solution:

You can use the known random function rand () to generate a new random function Rand () with equal probability of 0 and 1, and then call k (k is the number of digits represented by the binary integer n) the next Rand () function obtains a sequence of 0 and 1 with a length of k. The integer formed by this sequence is a number between 1 and n.

Note: The integer obtained from the generation sequence may be greater than n. If it is greater than n, It is regenerated until the obtained integer is not greater than n.

Step 1: The rand () function generates the Rand () function, and the Rand () function produces probabilities of 0 and 1.

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
Int Rand ()
{
While (1)
{
Int i1 = rand ();
Int i2 = rand ();
If (i1 = 0 & i2 = 1)
Return 1;
Else if (i1 = 1 & i2 = 0)
Return 0;
}
}

Step 2: Calculate the binary value of integer n to indicate the number of digits k, k = 1 + log2n (log base 2 n)

Step 3: Call k times Rand () to generate a random number. The resulting k 01 sequences represent numbers between 1 and N.

C ++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/6/3
*/
Int newRand ()
{
While (1)
{
Int result = 0;
For (int I = 0; I <k; ++ I)
{
If (Rand () = 1)
Result | = (1 <I );
}
If (result <= n)
Return result;
}
}

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/random-generator-with-equal-or-unequal-probability.html

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.