Thread: Rand() function...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Rand() function...

    Hey everyone, long time no speak ....

    I was reading Adrian's tutorial on the win32 console, and goofing around, and just wanted to make a program that would randomly plant characters on the screen, here's what I came up with:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    
    
    typedef struct COORD {
            short X;
            short Y;
            };
    
    
    
    
    short A;
    short B;
    
    int main() {
        int count;
        HANDLE hOut;
        COORD position;
    
        for (count = 0; count < 50; count++) {
    
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    srand(time(NULL));
    
                      A = 1 + (rand() % 40);
                      B = 1 + (rand() % 40);
                      position.X = A;
                      position.Y = B;
        SetConsoleCursorPosition(hOut, position);
                       printf("%d", count); //count is for debugging...
                                      }
                                      printf("Done");
                                      getchar();
    
    
    }
    The whole cursor position thing works fine, and the reason I put the variable count in the printf line is so I could see why it wasn't working...when I compile it as it stands now, it prints "49Done" on a random place on the screen. The good news is it's a random place everytime I start the program, however, I wanted 49 different random spots, for instance I had it to where it was "printf("*"); so I wanted 49 "*" 's on different parts of the screen, but I changed it to print COUNT so I could see what was going on, you would think right now it would print "1", <new random position>, "2", <new rand pos>....etc, but nope, it's just "49Done".....and I'm like, "what the heck happened to the other 48??, lol!"....

    Thanks a lot,

    Ash

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    #include <stdio.h>
    #include <windows.h>
    # include <time.h>
    #include <stdlib.h>
    
    
    short A;
    short B;
    
    
    typedef struct COORD
    {
    	short X;
    	short Y;
    };
    
    int main() 
    {
    	int count;
    
    	HANDLE hOut;
    
    		srand(time(0));
    	for (count = 0; count < 50; count++) 
    	{
    		COORD position;
    
    
    		A = 1 + (rand() % 40);
    		B = 1 + (rand() % 40);
    
    		position.X = A;
    		position.Y = B;
    
    		hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleCursorPosition(hOut, position);
    
    		printf("%c", count); //count is for debugging...
    	}
    	printf("Done");
    	return 0;
    }
    this worked for me

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    two things about your old code that I found:

    You didn't have included time.h, I think the srand(time(0)); random thing needs that to work, and the second thing is (I don't konw if this is right or not, but ... ) you were seeding the random thing inside the for() loop, if you try this code:

    Code:
    #include <stdio.h>
    #include <windows.h>
    # include <time.h>
    #include <stdlib.h>
    
    
    short A;
    short B;
    int main() 
    {
    	int count;
    	for ( count = 0; count < 10; count++) 
    	{
    		srand(time(0));
    		printf ("%d\n", count+rand()%10);
    	}
    	return 0;
    }
    you'll see that it isn't very random! it starts counting at a random value, but then it increments by one each time, for the incrementing value of count. It may have been writing over the origional thing you were writing ...

    I like the code tho!!!
    Last edited by twomers; 01-24-2006 at 03:39 AM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks for the input, and especially that you like the code, that's good to hear considering I'm still pretty new to all this, all I can say is I'm glad I left QB behind for a *real* programming language! (nothing against QB)

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Actually I realized that I needed a sleep() statement, the computer was just going waaay too fast...

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    
    typedef struct COORD {
            short X;
            short Y;
            };
    
    
    
    
    short A;
    short B;
    
    int main() {
        int count;
        HANDLE hOut;
        COORD position;
    
        for (count = 0; count < 50; count++) {
    
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    sleep(1000);
    srand(time(NULL));
    
                      A = 1 + (rand() % 40);
                      B = 1 + (rand() % 40);
    
                     position.X = A;
                     position.Y = B;
    
    
    
        SetConsoleCursorPosition(hOut, position);
                       printf("%d", count);
                                      }
                                      printf("Done");
                                      getchar();
    
    
    }
    works great now

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by Ash1981
    Actually I realized that I needed a sleep() statement, the computer was just going waaay too fast...
    That's because the random function is being seeded inside the for loop, it only needs to be seeded once, cause if you think about it, it can't really be all that more random than it is initially. if ya drag the Sleep() function outside, it'll go faster, and it will be random ...

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    oh ok thanks again

Popular pages Recent additions subscribe to a feed