How can I add probability?











up vote
0
down vote

favorite












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. :)










share|improve this question






















  • Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
    – Basile Starynkevitch
    Nov 20 at 14:51












  • @BasileStarynkevitch I get the impression you only read the title, not the question
    – lxop
    Nov 20 at 15:36










  • But the title is very important. If you feel it is not related to the question, change it
    – Basile Starynkevitch
    Nov 20 at 16:45















up vote
0
down vote

favorite












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. :)










share|improve this question






















  • Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
    – Basile Starynkevitch
    Nov 20 at 14:51












  • @BasileStarynkevitch I get the impression you only read the title, not the question
    – lxop
    Nov 20 at 15:36










  • But the title is very important. If you feel it is not related to the question, change it
    – Basile Starynkevitch
    Nov 20 at 16:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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. :)










share|improve this question













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. :)







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 14:41









obeeey

76




76












  • Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
    – Basile Starynkevitch
    Nov 20 at 14:51












  • @BasileStarynkevitch I get the impression you only read the title, not the question
    – lxop
    Nov 20 at 15:36










  • But the title is very important. If you feel it is not related to the question, change it
    – Basile Starynkevitch
    Nov 20 at 16:45


















  • Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
    – Basile Starynkevitch
    Nov 20 at 14:51












  • @BasileStarynkevitch I get the impression you only read the title, not the question
    – lxop
    Nov 20 at 15:36










  • But the title is very important. If you feel it is not related to the question, change it
    – Basile Starynkevitch
    Nov 20 at 16:45
















Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
– Basile Starynkevitch
Nov 20 at 14:51






Probabilities relate to math, not to C++ programming. In C++, you could add apples and oranges, but that might not make any sense.
– Basile Starynkevitch
Nov 20 at 14:51














@BasileStarynkevitch I get the impression you only read the title, not the question
– lxop
Nov 20 at 15:36




@BasileStarynkevitch I get the impression you only read the title, not the question
– lxop
Nov 20 at 15:36












But the title is very important. If you feel it is not related to the question, change it
– Basile Starynkevitch
Nov 20 at 16:45




But the title is very important. If you feel it is not related to the question, change it
– Basile Starynkevitch
Nov 20 at 16:45












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










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.






share|improve this answer

















  • 1




    This is biased and doesn't provide 15/85.
    – Matthieu Brucher
    Nov 20 at 14:49










  • Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
    – obeeey
    Nov 20 at 14:51








  • 1




    @obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
    – NathanOliver
    Nov 20 at 14:54










  • Okay, anyways thanks
    – obeeey
    Nov 20 at 14:55










  • Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
    – Useless
    Nov 20 at 14:55


















up vote
7
down vote













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.






share|improve this answer























  • Just curious. Why would rand be not advised?
    – Gill Bates
    Nov 20 at 14:49






  • 2




    @GillBates rand S****!. It can be very non-random.
    – NathanOliver
    Nov 20 at 14:50








  • 3




    @GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
    – François Andrieux
    Nov 20 at 14:51








  • 1




    It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
    – Matthieu Brucher
    Nov 20 at 14:52










  • STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
    – drRobertz
    Nov 20 at 14:52


















up vote
0
down vote














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





share|improve this answer





















  • Thank you for the idea :)
    – obeeey
    Nov 20 at 21:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395457%2fhow-can-i-add-probability%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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.






share|improve this answer

















  • 1




    This is biased and doesn't provide 15/85.
    – Matthieu Brucher
    Nov 20 at 14:49










  • Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
    – obeeey
    Nov 20 at 14:51








  • 1




    @obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
    – NathanOliver
    Nov 20 at 14:54










  • Okay, anyways thanks
    – obeeey
    Nov 20 at 14:55










  • Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
    – Useless
    Nov 20 at 14:55















up vote
0
down vote



accepted










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.






share|improve this answer

















  • 1




    This is biased and doesn't provide 15/85.
    – Matthieu Brucher
    Nov 20 at 14:49










  • Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
    – obeeey
    Nov 20 at 14:51








  • 1




    @obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
    – NathanOliver
    Nov 20 at 14:54










  • Okay, anyways thanks
    – obeeey
    Nov 20 at 14:55










  • Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
    – Useless
    Nov 20 at 14:55













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 14:45









lxop

2,78711022




2,78711022








  • 1




    This is biased and doesn't provide 15/85.
    – Matthieu Brucher
    Nov 20 at 14:49










  • Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
    – obeeey
    Nov 20 at 14:51








  • 1




    @obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
    – NathanOliver
    Nov 20 at 14:54










  • Okay, anyways thanks
    – obeeey
    Nov 20 at 14:55










  • Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
    – Useless
    Nov 20 at 14:55














  • 1




    This is biased and doesn't provide 15/85.
    – Matthieu Brucher
    Nov 20 at 14:49










  • Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
    – obeeey
    Nov 20 at 14:51








  • 1




    @obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
    – NathanOliver
    Nov 20 at 14:54










  • Okay, anyways thanks
    – obeeey
    Nov 20 at 14:55










  • Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
    – Useless
    Nov 20 at 14:55








1




1




This is biased and doesn't provide 15/85.
– Matthieu Brucher
Nov 20 at 14:49




This is biased and doesn't provide 15/85.
– Matthieu Brucher
Nov 20 at 14:49












Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
– obeeey
Nov 20 at 14:51






Wow, it was this easy... Thanks. Is there any chance that you can also help me with another step with my program? What should I do, to for example create a new bacteria (o) when there are 3 (o) around the blank space?
– obeeey
Nov 20 at 14:51






1




1




@obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
– NathanOliver
Nov 20 at 14:54




@obeeey The comments section is not the place to ask a new question. If you have a new question, as a new question.
– NathanOliver
Nov 20 at 14:54












Okay, anyways thanks
– obeeey
Nov 20 at 14:55




Okay, anyways thanks
– obeeey
Nov 20 at 14:55












Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
– Useless
Nov 20 at 14:55




Although since the answer is "assign the character literal 'o' to the array element you're talking about", I'm not sure it's worth a whole question either. Give it a try and see, I guess.
– Useless
Nov 20 at 14:55












up vote
7
down vote













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.






share|improve this answer























  • Just curious. Why would rand be not advised?
    – Gill Bates
    Nov 20 at 14:49






  • 2




    @GillBates rand S****!. It can be very non-random.
    – NathanOliver
    Nov 20 at 14:50








  • 3




    @GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
    – François Andrieux
    Nov 20 at 14:51








  • 1




    It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
    – Matthieu Brucher
    Nov 20 at 14:52










  • STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
    – drRobertz
    Nov 20 at 14:52















up vote
7
down vote













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.






share|improve this answer























  • Just curious. Why would rand be not advised?
    – Gill Bates
    Nov 20 at 14:49






  • 2




    @GillBates rand S****!. It can be very non-random.
    – NathanOliver
    Nov 20 at 14:50








  • 3




    @GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
    – François Andrieux
    Nov 20 at 14:51








  • 1




    It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
    – Matthieu Brucher
    Nov 20 at 14:52










  • STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
    – drRobertz
    Nov 20 at 14:52













up vote
7
down vote










up vote
7
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 15:22

























answered Nov 20 at 14:47









Matthieu Brucher

10.8k21935




10.8k21935












  • Just curious. Why would rand be not advised?
    – Gill Bates
    Nov 20 at 14:49






  • 2




    @GillBates rand S****!. It can be very non-random.
    – NathanOliver
    Nov 20 at 14:50








  • 3




    @GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
    – François Andrieux
    Nov 20 at 14:51








  • 1




    It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
    – Matthieu Brucher
    Nov 20 at 14:52










  • STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
    – drRobertz
    Nov 20 at 14:52


















  • Just curious. Why would rand be not advised?
    – Gill Bates
    Nov 20 at 14:49






  • 2




    @GillBates rand S****!. It can be very non-random.
    – NathanOliver
    Nov 20 at 14:50








  • 3




    @GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
    – François Andrieux
    Nov 20 at 14:51








  • 1




    It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
    – Matthieu Brucher
    Nov 20 at 14:52










  • STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
    – drRobertz
    Nov 20 at 14:52
















Just curious. Why would rand be not advised?
– Gill Bates
Nov 20 at 14:49




Just curious. Why would rand be not advised?
– Gill Bates
Nov 20 at 14:49




2




2




@GillBates rand S****!. It can be very non-random.
– NathanOliver
Nov 20 at 14:50






@GillBates rand S****!. It can be very non-random.
– NathanOliver
Nov 20 at 14:50






3




3




@GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
– François Andrieux
Nov 20 at 14:51






@GillBates The most common criticism I've encountered against randis that there is no requirement on how good it's "randomness" has to be, and as a consequence many implementations are bad (and they are allowed to be). Additionally, the rand() % x approach can introduce a bias towards certain values, making it not quite uniform.
– François Andrieux
Nov 20 at 14:51






1




1




It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
– Matthieu Brucher
Nov 20 at 14:52




It's very weak, when it works, global state... en.cppreference.com/w/cpp/numeric/random/rand
– Matthieu Brucher
Nov 20 at 14:52












STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
– drRobertz
Nov 20 at 14:52




STL gives an overview of why rand() is not good: channel9.msdn.com/Events/GoingNative/2013/…
– drRobertz
Nov 20 at 14:52










up vote
0
down vote














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





share|improve this answer





















  • Thank you for the idea :)
    – obeeey
    Nov 20 at 21:31















up vote
0
down vote














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





share|improve this answer





















  • Thank you for the idea :)
    – obeeey
    Nov 20 at 21:31













up vote
0
down vote










up vote
0
down vote










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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 17:41









2785528

4,27811017




4,27811017












  • Thank you for the idea :)
    – obeeey
    Nov 20 at 21:31


















  • Thank you for the idea :)
    – obeeey
    Nov 20 at 21:31
















Thank you for the idea :)
– obeeey
Nov 20 at 21:31




Thank you for the idea :)
– obeeey
Nov 20 at 21:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395457%2fhow-can-i-add-probability%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'