Thread: randomizing?

  1. #46
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    after just a quick look:
    Quote Originally Posted by cgod
    I changed your code so it works i hope this gives you a general idea of how to generate a "random" number next time

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     srand ( (unsigned int) time (0));
     int x = rand()%5;
     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;
    }
    1. if it's between 1 and 10, why are you using mod 5?
    2. you already have larger, smaller, and exactly covered, what else are you trying to do here?
    3. don't use system("pause"). ever.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #47
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Yeah ok but i just changed cerin's code so it would work he can do all the minor adjustments.I found this code from awhile ago maybe it will be usefull to cerin.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    	srand((unsigned)time(0));  // random number seed
    	int numberToGuess = rand()%5; // generate random number between 1-5
    	bool win = false;
    	int guessNumber = 0;
    
    
    	for (int numberGuesses = 0; numberGuesses < 5; numberGuesses++)
    	{
    		cout << "The computer has generate a random number try and guess it " <<endl;
    		cout << "Guess : ";
    		cin >> guessNumber;
    
    		if (guessNumber == numberToGuess)
    		{
    			cout << "Correct "<<endl;
    			win = true;
    			break;
    		}
    		else if (guessNumber > numberToGuess)
    		{
    			cout << "To high try a lower number"<<endl;
    			win = false;
    		}
    		else if (guessNumber < numberToGuess)
    		{
    			cout << "To low try a higher number "<<endl;
    			win = false;
    		}
    	}
    
    	if (!win)
    	{
    		cout << "You have lost the game try again "<<endl;
    	}
    
    	cin.get();
    	return 0;
    }

  3. #48
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by cgod
    Code:
    int numberToGuess = rand()%5; // generate random number between 1-5
    try again...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #49
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by major_small
    try again...
    Code:
    int numberToGuess = 2; // generate random number between 1-5
    My best code is written with the delete key.

  5. #50
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Trust Prelude to come up with something like that..

    Hm, I'm going to try doing Impossible Thing to Do #1.
    Just Google It. √

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

  6. #51
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    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.
    Trust me it does matter. Its easier to read, and when your using the [ PHP ]. It color codes it so its easier to tell whats, a comment, whats a function, ect.

  7. #52
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
     srand ( (unsigned int) time (0));
     int x = rand() / (RAND_MAX / 10 + 1 );
     (int)( (double) rand() / (RAND_MAX +1) * 10);
     int guess;
    It still doesn't change everytime I run it
    My computer is awesome.

  8. #53
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Might I make a recommendation????

    Go to a C/C++ textbook. Open it to page 1 and begin reading from there. The moment you try to gloss or skim over anything...STOP and re-read it again. You've obviously not grasped a single programming concept. Not even the most basic of principles or logic. And now you're turning a very very very simple statement into the most convoluted mess I've seen in a while. How on earth did you migrate from x = rand() % 10, to that indecipherable muck srand ( (unsigned int) time (0)); int x = rand() / (RAND_MAX / 10 + 1 ); (int)( (double) rand() / (RAND_MAX +1) * 10); int guess;?

    Good god man!

    Really, honestly I'm not tryin to be rude, but you've got to start from the beginning otherwise your confusion will just get worse. I've seen you struggling through other pieces of code in other threads, and I really mean it when I say I would rather see you progress and succeed instead of struggling and losing interest.
    Last edited by Scribbler; 02-02-2005 at 11:38 PM.

  9. #54
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    UMMMMMM...... really the first things I did were ACTUALLY READ some basic tutorials from http://www.geocities.com/tutorialperson/ (I'm on my mac laptop it won't let me make it a link) and then I READ the tutorials from this site.
    I'm working on getting a book. The code I had was posted and in the FAQ.
    My computer is awesome.

  10. #55
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I really hope this helps...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    using namespace std;
    
    
    int main()
    {
        //First, we seed the random number generator based on time with srand
        //so that we don't get the same random numbers every time
        srand( time(NULL) );
    
        //Now we assign a variable to a random number
        int foo = rand();
    
        //Now we assign a variable to a random number between 1-10
        int bar = (rand() % 10) + 1;
    
        //Now we print 10 different random numbers between 1- 100
        for( int x = 0; x < 10; x++ )
        {
            bar = (rand() % 100) + 1;
            cout <<bar <<endl;
        }
    
        cout <<'\n' <<endl;
    
        //Now, we have them guess a number between 1 - 10
        int number = (rand() % 10) + 1;
        int guess = 0;
    
        while( guess != number )
        {
            cout <<"Guess my number(between 1 - 10): ";
            cin >>guess;
    
            //Is guess between 1-10?
            if( guess < 1 || guess > 10 )
                cout <<"I said between 1-10, try again dude." <<endl;
            //Is it greater than our number?
            else if( guess > number )
                cout <<"Too high, guess lower." <<endl;
            //Is it lower than our number?
    
            else if( guess < number )
                cout <<"Too low, guess higher." <<endl;
        }
    
        //Since we've left the loop, we know they guessed correctly
        //...or there was a freak of nature, but we will ignore that case
        cout <<"You got it! Thanks for playing." <<endl;
    
    }
    This code is extremely straightforward, and if you still have questions after reading the FAQ, this post, and all the previous posts concerning the generation of random numbers and seeding the random generator, maybe programming isn't your bag, dude.

  11. #56
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright I got it now thanks for all the help. One of my teachers is going to look if she can find a mentor for me at the college.
    My computer is awesome.

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, 07:23 PM
  5. Randomizing
    By Morph in forum C++ Programming
    Replies: 18
    Last Post: 01-26-2003, 06:36 PM