17.4节练习

练习17.28 编写函数,每次调用生成并返回一个均匀分布的随机unsigned int。

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static()
{
	static default_random_engine e;
	static uniform_int_distribution<unsigned> u;
	 return u(e);
}
unsigned rand_()
{
	default_random_engine e;
	uniform_int_distribution<unsigned> u;
	return u(e);
}

int main()
{
	for (int i = 0; i < 10; ++i) {
		cout << rand_static() << endl;
		cout << rand_() << endl;
		cout << endl;
	}
}

练习17.29 修改上一题中编写的函数,允许用户提供一个种子作为可选参数。

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static(unsigned num)
{
	static default_random_engine e(num);
	//static uniform_int_distribution<unsigned> u;
	return e();
}
unsigned rand_(unsigned num)
{
	default_random_engine e(num);
	//uniform_int_distribution<unsigned> u;
	return e();
}

int main()
{
	unsigned num;
	cin >> num;
	for (int i = 0; i < 10; ++i) {

		cout << rand_static(num) << endl;
		cout << rand_(num) << endl;
		cout << endl;
	}

}

练习17.30 再次修改你的程序,此次再增加两个参数,表示函数允许返回的最小值和最大值。

#include <iostream>
#include <random>
using namespace std;
unsigned rand_static(unsigned num)
{
	static default_random_engine e(num);
	//static uniform_int_distribution<unsigned> u;
	cout << "max min:" << e.max() << " " << e.min() << endl;
	return e();
}
unsigned rand_(unsigned num)
{
	default_random_engine e(num);
	//uniform_int_distribution<unsigned> u;
	cout << "max min:" << e.max() << " " << e.min() << endl;
	return e();
}

int main()
{
	unsigned num;
	cin >> num;
	for (int i = 0; i < 10; ++i) {

		cout << rand_static(num) << endl;
		cout << rand_(num) << endl;
		cout << endl;
	}


}

练习17.31 对于本节中的游戏编程,如果在do循环体内定义be和e,会发生什么?

每步循环都会创建一个新引擎,从而每步循环都会生成相同的值。


练习17.32 如果我们在循环内定义resp,会发生什么?

作为循环的条件,不能定义在循环体呢,否则编译不通过。


练习17.33  修改11.3.6节(392页)中的单词转换程序,允许对一个给定单词有多种转换方式,每次随机选择一个进行实际转换。

    #include <iostream>  
    #include <fstream>  
    #include <sstream>  
    #include <set>  
    #include <map>  
    #include <random>  
    std::map<std::string, std::string> buildmap(std::ifstream &ifile) 
    {  
        std::map<std::string, std::string> temp;  
        std::string str, f, s;  
        while (getline(ifile, str))  
        {  
            std::istringstream sst(str);  
            sst >> f >> s;  
            temp[f] = s;        
        }  
        return temp;  
    }  
    std::string trans(const std::string &s, std::map<std::string, std::string> &m)//转换  
    {  
        auto temp = m.find(s);  
        if (temp != m.end())    
            return m[s];         
        else  
            return s;  
    }  
    std::string trans_random(const std::string &s, std::map<std::string, std::string> &m)//转换  
    {  
        auto temp = m.find(s);  
        if (temp != m.end())    
            return (m[s]+"!");    
        else  
            return s;  
    }  
    int main(int argc, char **argv)  
    {  
        using namespace std;  
        ifstream ifpw(argv[1]), iftxt(argv[2]);  
        if (!ifpw&&!iftxt)  
        {  
            cerr << "open file error!\n";  
            exit(1);  
        }  
        map<string, string> pw(buildmap(ifpw));    
        string temp, f;  
        default_random_engine e;  
        bernoulli_distribution b;  
        bool t;  
        while (getline(iftxt, temp))  
        {  
            istringstream iss(temp);  
            while (iss >> f)  
            {  
                t = b(e);  
                if(t)  
                    cout << trans_random(f, pw) << " ";  
                else  
                    cout << trans(f, pw) << " ";      
            }  
            cout << endl;  
        }  
        system("pause");  
        return 0;  
    }  


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值