Thread: Random rand() question

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Random rand() question

    Code:
    srand(time(NULL));    
    if(input == "gameover")
        {
                  
                random =(rand() % (6 - 1 + 1)) + 1;
                 
                 if( random == 1)
                 {
                 cout <<"You Stumble along the dirt road and find a penny\n";
                 money = money + 1;
                   if(counter >= 1)
                   {
                   counter = counter - 1;
                   }
                 }
                 if( random == 2)
                 {
                 cout <<"You reach into yer moms pocket and pull out a penny\n";
                 money = money + 1;
                   if(counter >= 1)
                   {
                   counter = counter - 1;
                   }
                 }
                 if(random == 3)
                 {
                 cout <<"As you search your couch you find a penny\n";
                 money = money + 1;
                   if(counter >= 1)
                   {
                   counter = counter - 1;
                   }
                 }
                 if(random == 4 && counter == 0)
                 {
                 cout <<"As you wander helplessly down the street you get knocked in the head.\n"
                      <<"You wake up later to find yourself in a alley and yer pockets empty\n";
                 money = 0;
                 counter = 5;
                 }
                 if(random == 5)
                 {
                 cout <<"You ask the mayor for bebe mooneys he gives u a penny\n";
                 money = money + 1;
                   if(counter >= 1)
                   {
                   counter = counter - 1;
                   }
                 }
                 if(random == 6)
                 {
                 cout <<"You Beg for mooneys and recieve a penny for a hard days work\n";
                 money = money + 1;
                   if(counter >= 1)
                   {
                   counter = counter - 1;
                   }
                 }
    For some reason that doesnt work for getting a number between 1 and 7 i dunt no how to get a number between that could someone point out how
    Maby it does maby its something else but sometimes when it types it doesnt respond no output so im thinking thiers a hole somewere
    Last edited by Nathan the noob; 01-27-2009 at 12:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like it should work. You might want to post the smallest and simplest compilable program that demonstrates the problem.
    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 2008
    Posts
    114
    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    
    using namespace std;
    
    int main()
    {
        int random;
        int max;
        int min;
        while(random > 0)
        {
                     cout << "Enter a max: ";
                     cin >> max;
                     cout << "Enter a min: ";
                     cin >> min;
        srand(time(NULL));             
        random =(rand() % (max - min + 1)) + min;
        cout <<"Hey the numbereno is " << random << "\n";
        }
    }
    It works here to :P but doesnt seem to in the acual place

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nathan the noob
    It works here to :P but doesnt seem to in the acual place
    Ah, so you know that you have done something right. Compare the code here to the code in the "actual place". Check that the input values are as expected (e.g., is min < max?)

    By the way, the pseudo-random number generator should be seeded once, not on each iteration.
    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

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    look at the top tell me if u see anything amiss

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nathan the noob
    look at the top tell me if u see anything amiss
    What happens if random == 4 and counter != 0?

    Other than that it looks fine, though I would have used an if-else if-else chain or a switch instead. It is also not necessary to have the number generated be in the range 1 to 5 inclusive since it would work just as well being in the range 0 to 4 inclusive.

    Oh, and remember to indent your code more consistently.
    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

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Oh thats so thatevent doesnt happen to often and some times that pice of code when typed gives no responce and im not sure why

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nathan the noob
    Oh thats so thatevent doesnt happen to often and some times that pice of code when typed gives no responce and im not sure why
    What do you mean by "some times that pice of code when typed gives no responce"? As far as I can tell, assuming that input == "gameover", something will always be printed, except in the case where random == 4 and counter != 0, but according to you that would be expected.
    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

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    wait i get the problem now
    its that when i t gets 4 and the counter is not 0 it doesnt respond well thanks any ways :P you were a big help

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    and
    Code:
    random =(rand() % (6 - 1 + 1)) + 1;
    is equivalent to
    Code:
    random = rand() % 6 + 1;
    why -1 + 1?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                 if( random == 1)
                 {
                    ...
                 }
                 if( random == 2)
                 {
                    ...
                 }
                 if(random == 3)
                 {
                    ...
                 }
                 if(random == 4 && counter == 0)
                 {
                    ...
                 }
                 if(random == 5)
                 {
                    ...
                 if(random == 6)
                 {
                    ...
                 }
    This could be written using a switch/case block instead:
    Code:
         switch(random)
         {
              case 1:
                 {
                    ...
                 }
                 break;
              case 2:
                 {
                    ...
                 }
                 break;
              case 3:
                 {
                    ...
                 }
                 break;
              case 4:
                 {
                    if (counter == 0)
                    ...
                 }
                 break;
    // etc.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Errrm thanks i am still looking for a way to use character or strings with switches would make stuff simpler

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nathan the noob View Post
    Errrm thanks i am still looking for a way to use character or strings with switches would make stuff simpler
    Well, you can't switch on a string (or character array) - for that, you probably want to build a table [such as vector, list or map] of a string and "something that tells you what to do" - the latter could be a functor object, a function pointer or simply a number that you then use a switch to find the "right action".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, for what it's worth, it's best if you only call srand() once during the execution of a program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM