Thread: need help dont understand

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    6

    need help dont understand

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int randrange(int low, int high){
    return rand()%(high-low+1)+low;
    }
    int main(){
    int a;
    srand(time(NULL));
    a = randrange(1,2);
    for ( int i=0;i<10; ++i)
        {
       cout<<randrange(1,2)<<'\n';
       switch (a){
       case 1:
           cout<<"heads\n";
           break;
    
       case 2:
        cout<<"tails\n";
        break;
       }
    
        }
    }

    i dont understand why it keep printing heads but not tails even when the random number is 2?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you do not assign to a within the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Thanks but after i moved it. The heads/ tails is random as well it dont follow 1= head 2=tails. Can ya tell me why is that ? I want to clearly understand this rand() before moving on .. Sorry i just start learning program like a week ago

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should cout the value of a, not the random generated number not related to current value of a
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    1
    You declared a = randrange(1,2) outside the loop, so it's different with the value of cout<<randrange(1,2) which is changing overtime within the loop, while a will always be the same value. That's why the result is either just heads or tails because the switch referred to the a value which was declared first. I've tried to solve it and it works fine.
    CMIIW because I'm also just start learning C++, so I want to ask if there's a specific reason of rand()%(high-low+1)+low ?
    Thanks.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ch1215 View Post
    CMIIW because I'm also just start learning C++, so I want to ask if there's a specific reason of rand()%(high-low+1)+low ?
    Thanks.
    You can try to read this article Generating Random Numbers in C and C++ - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Thanks i understand now great help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i dont understand this bug, please help me :(
    By Grey Kliche in forum C++ Programming
    Replies: 12
    Last Post: 08-09-2011, 08:03 AM
  2. i dont understand the problem
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 10-03-2008, 01:05 PM
  3. i dont understand bit
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 03-27-2008, 12:15 AM
  4. What I dont understand...
    By nomi in forum C# Programming
    Replies: 7
    Last Post: 01-20-2004, 02:00 AM
  5. dont understand VS.NET
    By Qui in forum Windows Programming
    Replies: 6
    Last Post: 10-15-2003, 07:36 PM