为什么生成的类甚至在随机后是相同的? [英] Why generated class is identical even after random?

查看:99
本文介绍了为什么生成的类甚至在随机后是相同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <vector>
#include <random>

using std::cout;
using std::cin;
using std::endl;
using std::ostream;

// Random value generator function
double randomize(const double &minVal, const double &maxVal){
	std::random_device rd;
	std::mt19937 genEngine(rd());
	std::uniform_real_distribution<double> distrib(minVal, maxVal);
	return distrib(genEngine);
}

class Warrior {
private:
	double _health;
	double _attack;

public:
	double HumanMaxAttrib = randomize(0.95,1.00) * 100;
    
   // Updated constructor (less flexibility when constructing)
    Warrior(){
        _health  = 100 * randomize(0.6,0.8);
        _attack  = 100 * randomize(0.6,0.8);;
    }

	// So that I could initialize with different strength.
	Warrior(const char &strength){
		if (strength == 'w'){
			_health   = HumanMaxAttrib * randomize(0.70,0.85);
			_attack  = HumanMaxAttrib * randomize(0.70,0.85);
		}

		if (strength == 'n'){
			_health   = HumanMaxAttrib * randomize(0.76,0.95);
			_attack  = HumanMaxAttrib * randomize(0.76,0.95);
		}

		if (strength == 's'){
			_health   = HumanMaxAttrib * randomize(0.86,1.00);
			_attack  = HumanMaxAttrib * randomize(0.86,1.00);
		}
	}

	double getHealth() const {return _health;}
	double getAttack() const {return _attack;}

	void setHealth (const double &health){_health = health;}
	void setAttack (const double &attack){_attack = attack;}

	ostream& toStream(ostream &os) const {
		cout<<"This is the Warrior attributes \n";
		cout<<"Health : "<<_health<<"\n";
		cout<<"Attack : "<<_attack<<"\n";
		return os;
	}
};

ostream& operator<<(ostream& os, const Warrior& v){
	return (v.toStream(os))  ;
}

constexpr char weak = 'w';
constexpr char normal = 'n';
constexpr char strong = 's';

int main(){

	Warrior war1(strong);
	cout<<war1;	// generate OK

	Warrior war2(normal);
	cout<<war2; // generate OK

	std::vector<Warrior> war3(3,Warrior(weak)); // generate identical. Why?
	for (auto i = 0; i < 3; i++) {
		cout<<war3[i];
	}

    // Using new constructor.
    std::vector<Human> itWorked(10);
    for (auto i = 0; i < 10; i++){     
    cout<<"Health: "<< itWorked[i].getHealth();
    cout<<"\nAttack: "<< itWorked[i].getAttack()<<endl<<endl;
    }

    return 0;
}





这里发生了什么?

我该怎么做才能生成矢量每次都是真正的随机类??



更新:产生不同结果的两个构造函数之间有什么不同?它对我来说看起来很无辜: - (



What has happened here?
What should I do to make the generated vector with truly random class each time??

Update: What so different between those two constructors that produce different result? It look innocent enough to me :-(

推荐答案

可能这是因为你以同样的方式行事:创造全新的 rd genEngine 对象每次你需要一个随机值。你需要在整个生命周期中只创建和初始化这些对象一次你的进程。例如,在 main 中创建这样的对象,并将指针传递给这个 genEngine ,或者你需要的任何东西使用,你的随机化作为另一个参数。



你可以在这里找到正确的代码示例: http://en.cppreference.com/w/cpp/numeric/random [ ^ ]。



请注意,对象 std :: random_device rd std :: mt19937 e2 仅构造/初始化一次,在 main



为了理解生成相同随机值的效果,请注意术语: -random https://en.wikipedia.org/wiki/Pseudorandom_number_generator [ ^ ]。



-SA
Probably this is because you act in identical way: create the brand new rd and genEngine objects each time you need a random value. You need to create and initialize these objects only once in the whole life cycle of your process. For example, create such objects in main and pass the pointer to this genEngine, or whatever you need to use, to your randomize as yet another parameter.

You can find correct code sample here: http://en.cppreference.com/w/cpp/numeric/random[^].

Note that the objects std::random_device rd and std::mt19937 e2 are constructed/initialized only once, in main.

For understanding the effect of generation of identical random value, pay attention for the term: pseudo-random: https://en.wikipedia.org/wiki/Pseudorandom_number_generator[^].

—SA


这篇关于为什么生成的类甚至在随机后是相同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆