About letting a loop know when to stop..

Hi, so.. i know the code below would produce 20 random numbers between 0 and 1.. but is there any way i can make the loop stop after having 3 consecutive numbers? like after having three 1's or three 0's in a row??
Thank you in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>
  #include <cstdlib>
  #include <ctime>

  int main()
  { 
    using namespace std;
    srand(time(0));
    for(int i= 0; i< 20; i++)
      {
         cout << rand()%2 << endl;
      }
    return;
  }

p.s i wish there was a command like while(until 3 consecutive nums) or something like that xD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include <iostream>
  #include <cstdlib>
  #include <ctime>

  int main()
  { 
    using namespace std;
    srand(time(0));
    int lastRanNum = -1;
    int runningCount = 0;
    int generatedSoFar = 0;

    while ((++generatedSoFar  < 20) &&
               runningCount < 3)
      {
         int newRanNum =  rand()%2;
         if (newRanNum  == lastRanNum )
         {
              runningCount++;
         }
         else
         {
             runningCount = 0;
             lastRanNum  = newRanNum;
          }
          
         cout << newRanNum   << endl;
      }
    return;
  }


Might be some off-by-one errors in the counts, but it should illustrate.
Last edited on
p.s i wish there was a command like while

These two are almost the same:
1
2
3
4
5
6
7
8
9
10
11
12
13
// A
for ( int i= 0; i< 20; ++i )
{
  // code
}

// B
int i= 0;
while (  i< 20 )
{
  // code
  ++i;
}


The condition expression can be complex in both.
1
2
3
4
for ( int i= 0; (i<20) && (runningCount<3); ++i )
{
  // code
}


It is also possible to "break out" from a loop "prematurely":
1
2
3
4
5
6
for ( int i= 0; i<20; ++i )
{
  // code
  if (runningCount<3) break;
  // code
}

I just tested the program, and it worked just as I wanted it to! Thank you so much Repeater and Keskiverto for the help!

(By the way, I had to change the 3 to 2 in the line below because when it was 3, the loop stopped after having 4 consecutive numbers instead of 3. But other than that, everything worked perfect! :D)
while ((++generatedSoFar < 20) && runningCount < 3
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//======================================

template <typename T> class repeatCounter
{
   int count = 0;
   int limit;
   T last;

public:
   repeatCounter( int mx ) { limit = mx; }

   bool add( T item )
   {
      if ( count == 0 || item != last ) { count = 1; last = item; }
      else                                count++;
      return count >= limit;
   }
};

//======================================

int main()
{ 
   repeatCounter<int> run(3);

   srand( time( 0 ) );
   for ( int i = 0; i < 20; i++ )
   {
      int digit = rand() % 2;
      cout << digit;
      if ( run.add( digit ) ) break;
   }
}

0100111
Last edited on
@lastchance
Oh wow that's something I haven't expected to see... Thank you so much!!


(Sorry I just checked your post..)
Topic archived. No new replies allowed.