Ex5-1-3 in Algorithm -- Algorithm

文章探讨了一个问题,即如何设计一个名为UNBIASED-RANDOM的算法,该算法使用BIASED-RANDOM子程序返回无偏的结果(即输出0的概率为1/2,输出1的概率为1/2)。虽然提供了C++代码框架,但未完成实际的算法实现,并且未给出预期运行时间与概率p的关系。
摘要由CSDN通过智能技术生成

Source of Book

For the textbook, see the note in Github.

Ex5-1-3

Q

The question is too long. So I don't type it. (I have NOT found the PDF version of textbook yet.)

Understand of Q (and changes)

Given a BIASED-RANDOM which output either 0 or 1 whereas the probability to output 0 is p (where 0 < p < 1 ). However, you don't know what actually p is. Give an algorithm (named UNBIASED-RANDOM) that uses BIASED-RANDOM as subroutines and return an unbiased answer (i.e. return 0 with probabilty 1/2, 1 with probabilty 1/2). What is the expected running time of your algorithm as a function p?

In my answer (shown in My Answer section ) of this article, it only answers the requirements of this section instead what the question in textbook asks (shown in Q section).

Answers

My Answer

Implementation 1

Code
C++
#include <cmath>
#include <string> // NOT <cstring>

// eps is 10^(-5)
#define eps ((double) (pow(10,-5)))
// only call BIASED_RANDOM(0,1).
int UNBIASED_RANDOM(double probability)
{
    string errorMessage = "ERROR!!! Out of range. Probabilty always should be between 0 and 1.";
    if(probability >= 1 - eps)
    {
        throw errorMessage;
    }
    if(probability <= 0 + eps)
    {
        throw errorMessage;
    }

    /*
    I quit I don't come with answer.
    */
}
Expected Running Time 
I don't know.

Other Answer

From the website,

Ch5.pdf (rutgers.edu)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值