Thread: Random Phrase

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Random Phrase

    I have been looking at teh tutorials for random numbers and was wondering if it was possible to get random phrases or would I just get a random number and if it =s something then post a certain phrase?


    And can I use a for loop witht this code from the FAQ?
    Code:
    #include <iostream>   
    #include <ctime>   
    
    int main(void)
    {
      int i;    
      
      srand(time(NULL));    
      i = rand();  
      std::cout <<"Your random number is " <<i <<std::endl;  
      std::cout <<"This compiler can generate random numbers from 0 to " 
                <<RAND_MAX <<std::endl;
      
      return(0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> would I just get a random number and if it =s something then post a certain phrase?

    Yes, that's how you would do it. To do so you need to get a number within the range of how many phrases you've created. There should be info on how to generate a number within a specific range in the FAQ. Then, use a switch or a map or an array/vector to determine which phrase is displayed based on the random number generated.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yes, I think the one in the FAQ over complicates things by increasing the range....so I'm not sure.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The formula is the important part. You can just use that to get a number in a range.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Code:
    #include <iostream>   
    #include <ctime>   
    
    int main(void)
    {
      int i;    
      
      for (i = 0; i < 20) 
     {
      srand(time(NULL));    
      i = rand();  
      std::cout <<"Your random number is " <<i <<std::endl;  
      }
      
      return(0);
    }
    Would that work? I have tried to adapt the code in teh FAQ

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Work for what? Getting a number in a range? No. First, srand() should be called once for the whole program, like you did it in your first piece of code from the FAQ. It is inside the for loop in your second piece of code, so you should put it back. Second, you don't need a loop to get a single random number within a range. That loop code is just an example to show how you can get a bunch of random numbers. Look for the Formula (marked as Formula in the code comments). Just replace the i = rand() in your first piece of code with the new way of calculating i. Don't forget that you have to change min and max in the formula to whatever values you want to be the min and max of your range.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    So I can set a RAND_MAX and RAND_MIN? Or how do I set a range?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you know the Formula I am talking about in the FAQ? Find that and read the comments and see if you can understand what I'm talking about. BTW, this is the FAQ I'm referring to:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Is that the one where you set the minimum and maximum in GetRand?

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Quote Originally Posted by FAQ
    /*
    * Formula:
    * rand() % N <- To get a number between 0 - N-1
    * Then add the result to min, giving you
    * a random number between min - max.
    */
    rc = (rand() % (max - min + 1) + min);
    Isn't that exactly what you are looking for?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's the code underneath the comment that says, "* Formula:".

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    rc = (rand() % (max - min + 1) + min);

    THat?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That is a formula for finding a random number within a range of values. I hope that's what you are looking for. That is the first step in outputting a random phrase.

    If you know what % does then you can figure out how it works.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I'm afraid I am too nooby to know what percent does...unless I have overlooked it. Can I just use int min and int max to go in that function thing?

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by bumfluff
    I'm afraid I am too nooby to know what percent does...unless I have overlooked it. Can I just use int min and int max to go in that function thing?
    What do you think that function is? Can't you see it's user-defined at the bottom of the program with that formula?

    % is an integer operator that returns the remainder of division

    4 % 2 = 0
    5 % 2 = 1
    11 % 3 = 2

    So you call rand, you mod it by the max number and get the remainder (which is always smaller than your divisor) and subtract your mininum number + 1. Then you add to all that your minimum.

    rand() % (max - min + 1) + min
    Sent from my iPadŽ

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