obeeey
obeeey

Reputation: 113

How can I add probability?

it's my firt post here.

I have done this:

#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;
}

The language is polish, so don't be confused with the terminology.

I'm trying to make Conway's "Game of life", it's only the beginning for now, and I do not really know what to do next. Right now, my problem is how to set a probability of drawing "o" to 15% vs 85% for blank space ""?

If it's possible, I just want to make a little change in my code, not doing something really ambitious and unknown, because probably I wouldn't understand it.

I hope there is someone who's willing to help me with this. :)

Upvotes: 1

Views: 449

Answers (3)

2785528
2785528

Reputation: 5566

Right now, my problem is how to set a probability of drawing "o" to 15% vs 85% for blank space ""?

Another possibility. I like the following because this approach can easily show the distribution, and is easily modified.

#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()(); }

With possible output:

  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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~~~~~~~o~~~~~~~~~~~~~~~~~o~~~~~~~~~~~~~~o

Upvotes: 0

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

You should use the random header (rand is not advised):

#include <random>

And then in your function:

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;

Then check if dist(rng) is lower than 15 to have your percentage.

Of course, the dist and the rng should be outside your generujpole function and be passed in.

Upvotes: 8

lxop
lxop

Reputation: 8595

You have basically got it already, except at the moment you are limiting yourself to 50% probability with the %2 part.

Try increasing that to % 100, and instead of comparing the result with == 0, compare with < 15, which will occur approximately 15% of the time.

Upvotes: 0

Related Questions