title: Randomly selecting enum values according to few rules
body: <p>Please have a look at the following code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main()
  {
      enum Movement{STAND,WALK,RUN,CRAWL};
  
      Movement state = (Movement)(1+rand()%4);
  
      for(int i=0;i&lt;100;i++)
      {
  
      cout &lt;&lt; state &lt;&lt; endl;
  
      switch(state)
      {
          case STAND: 
              cout &lt;&lt; "You can walk or crawl" &lt;&lt; endl;        
              while(state==WALK || state==CRAWL){
              state = (Movement)(1+rand()%4);}
              break;
  
  
          case WALK: 
              cout &lt;&lt; "You can stand or run" &lt;&lt; endl;
              while(state==STAND || state==RUN){
              state = (Movement)(1+rand()%4);}
              break;
  
  
          case RUN: 
              cout &lt;&lt; "You can walk" &lt;&lt; endl;
              while(state==WALK){
              state = (Movement)(1+rand()%4);}
              break;
  
          default: 
              cout &lt;&lt; "You can stand" &lt;&lt; endl;
              while(state==STAND){
              state = (Movement)(1+rand()%4);}
  
      }
  
      }
  }
  </code></pre>
  
  <p>In here, there are few rules. Lets call them as legal transitions</p>
  
  <ol>
  <li>From stand, he can walk or crawl</li>
  <li>From Walk, he can stand or run</li>
  <li>From Run, he can walk</li>
  <li>From Crawl, he can stand</li>
  </ol>
  
  <p>So, here, if the legal transitions should be randomly selected. But, however, it should be according to the rules provided above. For an example, if "STAND" is selected, next thing to be selected should be "WALK" or "CRAWL" likewise. But as you can see here, all the result</p>
  
  <pre><code>2
  You can walk
  </code></pre>
  
  <p>Why is that? Please help!</p>
  
  <p>UPDATE:</p>
  
  <p>Following code is with do..while loop, as suggested in a reply</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main()
  {
      enum Movement{STAND,WALK,RUN,CRAWL};
  
      Movement state = (Movement)(1+rand()%4);
  
      for(int i=0;i&lt;10;i++)
      {
  
      cout &lt;&lt; state;
  
      if(state==STAND)
      {
          cout &lt;&lt; " STAND is selected" &lt;&lt; endl;
  
      }
      else if(state==WALK)
      {
          cout &lt;&lt; " WALK is selected" &lt;&lt; endl;
  
      }
      else if(state==RUN)
      {
          cout &lt;&lt; " RUN is selected" &lt;&lt; endl;
      }
      else if(state==CRAWL)
      {
          cout &lt;&lt; " CRAWL is selected" &lt;&lt; endl;
      }
  
      switch(state)
      {
          case STAND: 
              //cout &lt;&lt; "You can walk or crawl" &lt;&lt; endl;        
              do{state = (Movement)(rand()%4);}
              while(state==WALK || state==CRAWL);
  
              break;
  
  
          case WALK: 
              //cout &lt;&lt; "You can stand or run" &lt;&lt; endl;
              do{state = (Movement)(rand()%4);}
              while(state==STAND || state==RUN);
              break;
  
  
          case RUN: 
              //cout &lt;&lt; "You can walk" &lt;&lt; endl;
              do{state = (Movement)(rand()%4);}
              while(state==WALK);
              break;
  
          default: 
              //cout &lt;&lt; "You can stand" &lt;&lt; endl;
              do{state = (Movement)(rand()%4);}
              while(state==STAND);
  
      }
  
      }
  }
  </code></pre>
  
  <p>It is still the same. Now I get different answers, but not correct ones!!</p>