这是我的第一篇文章。
我已经做到了:
#include <iostream>
#include <ctime>
using namespace std;
const int width = 60;
const int height = 20;
void generujpole(char pole[][height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
int maluj = rand()%2;
if(maluj == 0) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}
void wypiszpole(char pole[][height])
{
cout << "+------------------------------------------------------------+"<<endl;
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
}
int main()
{
srand(time(NULL));
char plansza[60][20];
generujpole(plansza);
wypiszpole(plansza);
return 0;
}
语言是波兰语,因此请不要与术语混淆。
我正在尝试打造康威的“生活游戏”,这只是目前的开始, 我真的不知道下一步该怎么做。现在,我的问题是如何将绘制“ o”的概率设置为15%,而空白“”的概率为85%?
如果可能的话,我只想对我的代码做些改动,不要做一些雄心勃勃且未知的事情,因为我可能不理解。
我希望有人愿意为此提供帮助。 :)
答案 0 :(得分:7)
您应该使用random
标头(不建议使用rand
)
#include <random>
然后在您的函数中:
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> dist(0,99); // distribution in range [0, 100[
std::cout << dist(rng) << std::endl;
然后检查dist(rng)
是否小于15以得到您的百分比。
当然,dist
和rng
应该在您的generujpole
函数之外并传递。
答案 1 :(得分:0)
您基本上已经掌握了这一点,除了目前%2
部分将自己限制为50%的概率。
尝试将其增加到% 100
,而不是将结果与== 0
进行比较,而与< 15
进行比较,这大约会发生15%。
答案 2 :(得分:0)
现在,我的问题是如何设置将“ o”绘制为 15%和85%的空白“”?
另一种可能性。我喜欢以下内容,因为这种方法可以轻松显示分布并易于修改。
#include <iostream>
using std::cout, std::endl;
#include <iomanip>
using std::setw;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
using std::shuffle;
#include <random> // random_device
using std::random_device, std::mt19937_64;
class T990_t
{
vector<char>::size_type sz100;
vector<char>::size_type percent15; // 15 percent
vector<char>::size_type percent85; // 85 percent
vector<char> gameBoard;
public:
T990_t() : sz100 (20*60) , percent15 (0) , percent85 (0), gameBoard()
{
gameBoard.reserve(sz100);
}
~T990_t() = default;
int operator()() { return exec(); }
private: // methods
int exec()
{
// initialize the gameBoard to DEAD state
for (vector<char>::size_type i=0; i < sz100; ++i)
gameBoard.push_back('~'); // DEAD state (using visible char)
// how might we set 15% of gameboard to ALIVE?
setGameBoardAlive ( 15U );
showGameBoard (" at 15% (note poor (but valid) sequence) ");
// how 'distribute' the 15% ?
shuffleGameBoard();
showGameBoard(" after shuffle ");
return 0;
}
void setGameBoardAlive(vector<char>::size_type percentAlive)
{
percent15 = ( sz100 * percentAlive ) / 100; // 15 percent
percent85 = ( sz100 - percent15 ); // 85 percent
cout << "\n 100 percent: " << setw(5) << sz100
<< "\n 15 percent: " << setw(5) << percent15
<< "\n 85 percent: " << setw(5) << percent85
<< "\n sum : " << setw(5) << (percent15 + percent85)
<< endl;
for (vector<char>::size_type i = 0; i < percent15; i++) {
gameBoard[i] = 'o'; // ALIVE state
}
}
void showGameBoard(string lbl)
{
int ll = 0;
cout << "\n GameBoard " << lbl << ": \n ";
for (vector<char>::size_type i=0; i<sz100; i++)
{
cout << gameBoard[i];
ll += 1;
if (ll > 100) { cout << "\n "; ll = 0; }
}
cout << endl;
}
void shuffleGameBoard()
{
cout << "\n gameBoard std::shuffle \n ";
random_device rd;
mt19937_64 gen(rd());
shuffle (gameBoard.begin(), gameBoard.end(), gen);
//cout << "\n ";
}
}; // class T990_t
int main(int , char**) { return T990_t()(); }
可能的输出:
100 percent: 1200
15 percent: 180
85 percent: 1020
sum : 1200
GameBoard at 15% (note poor (but valid) sequence) :
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~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