# using rand() function

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

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

thousand thanks
i solve it

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632169
• Total Posts
3004545

×