Sign in to follow this  
lcy

using rand() function

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this