• Advertisement
Sign in to follow this  

using rand() function

This topic is 4687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do i print L and W in randowly ? lets say T is the token and P is the probability N is the number series if the input T = 20, P = 20%, N = 3 and the the output should like this LLLWLLWLLLWLLWLLLLWL WWLLLLLLLLLLWLLLLWLL LWLLWLWLLLLLLWLLLLLL mine program like this T = T*((P)/100); for(int i = 1; i <= N; i++) { cout<<"\n"; for(int j = 1; j <= T; j++) cout<<'W'; } i was only able print the W and i don't know how to print the rest of the L in randowly please help

Share this post


Link to post
Share on other sites
Advertisement
I'm not entirely certain I follow your notation, but I believe you're looking for something like this:

bool bernoulli(double success)
{
//map the output of rand() to be in [0, 1)
return rand() / double(RAND_MAX+1) < success;
}

int main()
{
int N = 3;
int T = 20;
for(int i = 0; i < N; j++) {
for(int j = 0; j < T; j++) {
cout << (bernoulli(0.2) ? 'W' : 'L');
}
cout << endl;
}
}

CM

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement