CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    printing some random numbers (that aren't)

    Hello,

    I am writing some non-simple output from code where I may not have cout so I wrote a test program to try with printf().

    Code:
    // test print with printf()
    #include <stdio.h>   // for printf()
    #include <cstdlib>   // for rand()
    #include <iostream>  // for cout
    
    using namespace std;
    
    int main() {
    
       // declare loop iterator
       unsigned int i;
    
       // declare array of int
       int vars[8] = { 0 };
    
       // populate array with random int
       for(i=0; i<8; i++) { vars[i] = rand(); }
    
       // test print array values
       for(i=0; i<8; i++) {
          printf("vars[");
          printf("%d", i);
          printf("] = ");
          printf("%d \n", vars[i]);
       }
       printf("\n");
    
       //cout version of print
       // for(i=0; i<8; i++) { cout << "vars[" << i << "] = " << vars[i] << endl; }
       // cout << endl;
    
    return 0;
    }
    This prints the expected output of the random ints,

    vars[0] = 1804289383
    vars[1] = 846930886
    vars[2] = 1681692777
    vars[3] = 1714636915
    vars[4] = 1957747793
    vars[5] = 424238335
    vars[6] = 719885386
    vars[7] = 1649760492

    I have two questions,

    1. is there a simpler way to write the printf() code so that it is more like the one line cout version? I have tried but haven't had any success. It's not terribly important but I would like to understand printf() formatting better.

    2. every time I run this I get the same "random" numbers which means something is badly wrong. I'm not sure what this suggests. If it was just printing what was at the memory address for vars[0], etc, interpreted as an int, the numbers would likely be very large or very small and not nice looking ints like above.

    Thanks again,

    LMHmedchem
    Last edited by LMHmedchem; September 21st, 2024 at 11:39 AM.

  2. #2
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    Re: printing some random numbers (that aren't)

    While I added a second statement to re-create the random numbers,

    Code:
    // test printf()
    #include <stdio.h> // for printf()
    #include <cstdlib> // for rand()
    
    using namespace std;
    
    int main() {
    
       // declare loop iterator
       unsigned int i;
       // declare array of int
       int vars[8] = { 0 };
       // populate array with random numbers
       for(i=0; i<8; i++) { vars[i] = rand(); }
    
       // test print array values
       for(i=0; i<8; i++) {
          printf("vars[");
          printf("%d", i);
          printf("] = ");
          printf("%d \n", vars[i]);
       }
       printf("\n");
    
       // reinitialize array
       // populate array with random numbers
       for(i=0; i<8; i++) { vars[i] = 0; vars[i] = rand(); }
    
       // test print array values
       for(i=0; i<8; i++) {
          printf("vars[");
          printf("%d", i);
          printf("] = ");
          printf("%d \n", vars[i]);
       }
       printf("\n");
    
    return 0;
    }
    Printing the array after the second randomization shows different numbers,

    vars[0] = 1804289383
    vars[1] = 846930886
    vars[2] = 1681692777
    vars[3] = 1714636915
    vars[4] = 1957747793
    vars[5] = 424238335
    vars[6] = 719885386
    vars[7] = 1649760492

    vars[0] = 596516649
    vars[1] = 1189641421
    vars[2] = 1025202362
    vars[3] = 1350490027
    vars[4] = 783368690
    vars[5] = 1102520059
    vars[6] = 2044897763
    vars[7] = 1967513926

    but again, I get the same values for both sets if I run it over and over again.

    Since I thought that was odd, I did a bit more research. This post,

    https://cplusplus.com/forum/beginner/230838/

    indicates that I forgot to seed the random number generator. Adding srand(time(0)); before the first call to rand() fixes the issue. I also added <ctime> to the includes though the <cstdlib> include is probably sufficient.

    This is the final code.

    Code:
    // test printf()
    #include <stdio.h> // for printf()
    #include <cstdlib> // for rand()
    #include <ctime>   // for srand()
    
    using namespace std;
    
    int main() {
    
       // declare loop iterator
       unsigned int i;
       // declare array of int
       int vars[8] = { 0 };
    
       // seed the random number generator
       srand(time(0));
    
       // populate array with random numbers
       for(i=0; i<8; i++) { vars[i] = rand(); }
    
       // test print array values
       for(i=0; i<8; i++) {
          printf("vars[");
          printf("%d", i);
          printf("] = ");
          printf("%d \n", vars[i]);
       }
       printf("\n");
    
       // reinitialize array and re-populate with random numbers
       for(i=0; i<8; i++) { vars[i] = 0; vars[i] = rand(); }
    
       // test print array values
       for(i=0; i<8; i++) {
          printf("vars[");
          printf("%d", i);
          printf("] = ");
          printf("%d \n", vars[i]);
       }
       printf("\n");
    
    return 0;
    }
    I am still interested in learning about the printf() format if anyone can answer that.

    LMHmedchem
    Last edited by LMHmedchem; September 21st, 2024 at 02:14 PM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: printing some random numbers (that aren't)

    Re printf(). You can have a list of args to display following the "". % will use the args from the left. Try:

    Code:
    printf("vars[%d] = %d\n", i, vars[i]);
    You need to include ctime for time(). Note that if you're using ctime and cstdlib then this is for c++ and you also use cstdio instead of stdio.h.

    Also, if you just want to output a single char (line \n) then you can simply use putchar()

    Code:
    putchar('\n');
    The initialise and print code could be placed in function and called twice:

    Code:
    #include <cstdio> // for printf()
    #include <cstdlib> // for rand()
    #include <ctime>   // for time()
    
    using namespace std;
    
    void doarray() {
    	const unsigned arsz = 8;
    
    	// declare array of int
    	int vars[arsz] = { 0 };
    
    	// populate array with random numbers
    	for (unsigned i = 0; i < arsz; i++)
    		vars[i] = rand();
    
    	// test print array values
    	for (unsigned i = 0; i < arsz; i++)
    		printf("vars[%d] = %d\n", i, vars[i]);
    
    	putchar('\n');
    }
    
    int main() {
    	// seed the random number generator
    	srand(time(0));
    
    	doarray();
    	doarray();
    }
    Note that return 0 is not required in main() as this is defaulted if not present.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured