randomizing?

This is a discussion on randomizing? within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <stdlib.h> using namespace std; int main(int argc, char *argv[]) { int number = 8; int guess; do ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Question randomizing?

    #include <iostream.h>
    #include <stdlib.h>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int number = 8;
    int guess;
    do {
    cout << "im thinking of a number between 1 and 10\n";
    cout << "enter your guess please\n";

    cin >> guess;

    if (guess == number) {
    cout << "you are correct\n";
    }
    else if (guess < 8) {
    cout << "guess a higher number\n";
    }
    else if (guess > 10) {
    cout << "your not supposed to guess this high\n";
    }
    else {
    cout << "guess a lower number\n";
    }
    }while (guess != 8);

    system("pause");
    return 0;
    }

    Could someone kindly tell me how I can make it so that the guess is randomized?

    Oh ya, I found a site that explains c++ in for actual BEGINNERS with no programming background. Could someone also explain to me what the stuff in the parentheses mean?int main(int argc, char *argv[])
    Last edited by cerin; 01-04-2005 at 02:28 AM. Reason: forgot sumtin

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You might want to review some points of the FAQ. Especially the "About the forums and netiquette" point (subpoint "How do I format my code"), and the "Generate random numbers" point in "How do I, Level 1".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    ever thought about if the user puts in a negative number from your program, it would say "Go lower", but if its lower, then the user input will already be out of the range previously stated. but to get a "random" number, try...
    int x = (rand()%9)+1;
    and then in the if statements put is things like
    if(guess < x){ cout << "guess a higher number" << endl; }
    Last edited by Finchie_88; 01-04-2005 at 09:44 AM.


  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I don't quite see what your saying. When I enter a negative number it says go higher.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Woop?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    ok i got that now stop posting about it.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    int x = (rand()%9+1;
    int guess;
    do {
    cout << "im thinking of a number between 1 and 10\n";
    cout << "enter your guess please\n";
    
    cin >> guess;
    
    if (guess == x) {
    cout << "you are correct\n";
    }
    else if (guess < x) {
    cout << "guess a higher number\n";
    }
    else if (guess > x) {
    cout << "your not supposed to guess this high\n";
    }
    else {
    cout << "guess a lower number\n";
    }
    }while (guess != x);
    
    system("pause");
    return 0;
    }
    The faq doesn't help me I don't know C and thats what it explains it in. Am I suppose to do something like this?
    My computer is awesome.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>The faq doesn't help me I don't know C and thats what it explains it in.
    C is mostly just a subset of C++. Anyway, the essential logic and syntax of the thing is the same:

    Code:
    #include <ctime>
    #include <cstdlib>
    
    int main()
    {
       srand(time(NULL));
       ...
    
       const int min = 100;
       const int max = 200;
       int randomNumber = (((double)rand() / (double)(RAND_MAX + 1)) * (max - min)) + min;
    This is a better method than the one involving %, but still isn't perfect. For more info, check out Prelude's corner in the FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >The faq doesn't help me I don't know C and thats what it explains it in.
    The only difference would be including <cstdlib> instead of <stdlib.h>. Of course, you could have used your lack of C experience as an excuse to begin learning C instead of complaining about what you don't know.

    >Am I suppose to do something like this?
    Something like that, yes. Here's a fun one:
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int n, guess;
      const char *msg[] = {
        "Too low",
        "Too high",
      };
    
      srand((unsigned int)time(0)); // IFFY: Not portable
    
      cout<<"I'm thinking of a number between 1 and 100...\n";
      n = rand() % 100 + 1; // IFFY: Low-order bits may not be random
      cout<<"Try to guess what it is: ";
    
      while (cin>> guess && guess != n)
        cout<< msg[guess > n] <<" try again: ";
    
      if (cin)
        cout<<"You got it!"<<endl;
    }
    Kampai!

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Hmmm

    I cant belive some of you comment, that people dont post there code correctly, i mean at the end of the day, it dont matter.

  11. #11
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by chrismax2
    I cant belive some of you comment, that people dont post there code correctly, i mean at the end of the day, it dont matter.
    Without code tags, it's annoying to try to distinguish code from the rest of the post and it looks stupid too.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I cant belive some of you comment, that people dont post there code correctly, i mean at the end of the day, it dont matter.
    Sure, if it's 2 lines then go ahead and post without code tags. But anything more than that, and it really DOESN'T matter what code they've shown, because nobody's going to look at it without code tags.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You don't always have to follow the rules cerin, I'll always help ya anyways.

    > Could someone also explain to me what the stuff in the parentheses mean?int main(int argc, char *argv[])
    argc = The number of arguements.
    argv = The actual arguements

    Try this exercise:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(
       int argc,      // The number of arguements
       char **argv )  // The arguement strings
    {
       // Loop through all of the arguements:
       
       int i;
       for( i=0; i<argc; i++ )
          puts( argv[i] );
    
       // Return success
    
       return 0;
    }
    Output:
    Code:
    Shiva:/home/kleid/Programming/Laboratory# gcc argvargc.c -Wall
    Shiva:/home/kleid/Programming/Laboratory# ./a.out
    ./a.out
    Shiva:/home/kleid/Programming/Laboratory# ./a.out hello cerin how are you?
    ./a.out
    hello
    cerin
    how
    are
    you?

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Yeah and I will always help as well

    I noticed on here over the years people take these rules as gospel, well I say that if people dont want to put tags then thats fine with me, I'll read it with pleasure!

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm glad you're so willing to help. However, the point is that code without tags is much harder to read. If you're not going to spend 3 seconds putting on the code tags that will save us 10 minutes of headache while figuring out your code, then why do we (those of us who aren't so philanthropic) want to help you? We realize that around here people will forget code tags, or first-time posters don't know to put them on, so we ask them politely and give a link to the code-tag sticky. Once the code tags are added (and therefore the code becomes readable), then we seriously consider helping out.

    And if you say "the code looks the same without code tags anyway", then that is an indicator that you suck at writing properly indented/formatted code.

    On topic:
    >>Could someone also explain to me what the stuff in the parentheses mean?
    It's a mechanism by which arguments from the command line can be processed by your program. argc is short for 'argument count', argv is short for 'argument variables' or something similar.. but you can call them anything you want instead. For example, if you run your program like this:
    Code:
    myprog.exe -cheats 1 -level 99
    Then argc == 4, argv[0] == "-cheats", argv[1] == "1", argv[2] == "-level", argv[3] == "99".

    Kleid-0 gave an example of how you can parse the arguments. There are several different ways you can declare main() to accept these arguments - Kleid-0 did it one way, you did it another, but both will (should?) work fine. On the other hand, if you don't plan to touch the command-line arguments, then you can simply declare main like this:
    Code:
    int main(void)
    {
       //...
    }
    //or
    int main()
    {
       //...
    }
    That way your program just ignores all command-line arguments.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomizing dealing program C
    By BSmith4740 in forum C Programming
    Replies: 2
    Last Post: 08-04-2008, 01:42 PM
  2. randomizing
    By zanderela in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 01:54 AM
  3. Randomizing link list.
    By Axel in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 10:03 AM
  4. srand() not randomizing me?!
    By crummy in forum C Programming
    Replies: 3
    Last Post: 02-11-2005, 06:23 PM
  5. Randomizing
    By Morph in forum C++ Programming
    Replies: 18
    Last Post: 01-26-2003, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21