Nick help

This is a discussion on Nick help within the C++ Programming forums, part of the General Programming Boards category; <<split from some other thread>> Hey guys I attempted this program for fun and was wondering what I need to ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Exclamation Nick help

    <<split from some other thread>>
    Hey guys I attempted this program for fun and was wondering what I need to change for this to work properly? Currently it will display one random number at a time a whole bunch of times then when it hits zero it will always say count is 25.

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    int main ()
    
    {
        
        
        int random, count, i;
        
        
        for (i = 1; ; ++i){
            srand(time(NULL));
            random = rand() % 10;
            cout << random << endl;
            
            
            if(random == 0){
            break;
            }
            }
            
            while (i == random){
                  count++;
                  }
                  
                  cout << count;
            
            system ("pause");
            
            return 0;
            }
    Last edited by Salem; 04-20-2010 at 11:07 AM. Reason: splitting

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want the count to happen inside the loop, you should put it ... inside the loop. And initially count is 25 (apparently) since you don't bother to ever give it a real value.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by tabstop View Post
    If you want the count to happen inside the loop, you should put it ... inside the loop. And initially count is 25 (apparently) since you don't bother to ever give it a real value.
    When i put count inside the loop it makes the program perform even more strange, it just prints about 3 numbers and then the program just sits there with a blinking cursor as if its still doing something in the background. any other suggestions?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by nick753 View Post
    Code:
            while (i == random){
                  count++;
                  }
    What is this meant to do?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Exclamation moving

    Do not call srand() more than once in one program, there is no point, especially in a fast loop where time() will be the same everytime (the measure is in seconds).

    I notice that the way my RNG works, having that srand() inside the for loop causes it to output the same number repeatedly. Moving it to where it should be:

    Code:
            srand(time(NULL));
        
        for (i = 1; ; ++i){
            random = rand() % 10;
    Made a drastic difference.

    This illustrates why you should not seed more than once. Every srand() call resets the generator, so if you call it like this:
    Code:
    	for (i=0;i<10;i++) {
    		srand(22222);
    		printf("%d\n", rand());
    	}
    You will get the exact same number 10 times. Now remember that time() will be the same for an entire second in your code.
    Last edited by MK27; 04-20-2010 at 11:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by DeadPlanet View Post
    What is this meant to do?
    count how many random numbers it took to get to zero. What is wrong?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    nevermind guys, I used a nested loop. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with my function (Heh)
    By phatsam in forum C Programming
    Replies: 9
    Last Post: 03-23-2006, 01:26 PM
  2. the new nick
    By NickESP in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:26 AM
  3. Differences in Debug and Release builds in MSVC
    By TravisS in forum C Programming
    Replies: 5
    Last Post: 06-19-2002, 11:40 PM
  4. try your heads on this
    By jasrajva in forum A Brief History of Cprogramming.com
    Replies: 70
    Last Post: 11-08-2001, 11:31 PM

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