Help generating random numbers in MFC

This is a discussion on Help generating random numbers in MFC within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have a function to try and put random numbers into an array. However for each object that ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    Help generating random numbers in MFC

    Hi all, I have a function to try and put random numbers into an array. However for each object that I run the function for the same random numbers are being produced. I used time.h to try and seed SRand with that to no avail, I have also tried srand(GetTickCount()); and also GetSystemTime. Please help as this is essential to the rest of my program. Below is the code for the function:
    m_XYCoOrds are numbers between 0 and 20.
    m_DrawCoOrds are these numbers converted into a form which will be representative when drawn to a window (the y axis are upside down and i wanted a bigger scale)

    void CLine::RandomCoOrd()
    {
    SYSTEMTIME util;
    GetSystemTime(&util);

    m_XYCoOrds[0][0] = 2; //start point
    m_XYCoOrds[0][1] = 18;
    m_XYCoOrds[9][0] = 17;//end point
    m_XYCoOrds[9][1] = 5;

    m_DrawCoOrds[0][0] = 60;
    m_DrawCoOrds[0][1] = 60;
    m_DrawCoOrds[9][0] = 360;
    m_DrawCoOrds[9][1] = 320;

    int j = 0; // y axis counter
    //srand( util( NULL ) ); // initialize seed "randomly"
    srand(GetTickCount());
    for(int i=1; i<9; i++)
    {

    int r = rand() % 21; // generate a random number lower than 20
    m_XYCoOrds[i][j] = r;//set the random number as the line coords
    r = ((r*20)+20); //convert it to drawable coordinates
    m_DrawCoOrds[i][j] = r;

    }

    j = 1;
    for( i=1; i<9; i++)
    {

    int r = rand() % 21; // generate a random number lower than 20
    m_XYCoOrds[i][j] = r;//set the random number as the line coords
    r = (((20-r)*20)+20); //convert it to drawable coordinates
    m_DrawCoOrds[i][j] = r;

    }


    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Read up on the srand() function. It can make your rand() function more random.

    And in the future, use code tags.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    Sorted

    Not sure what he was on about with code tags. If anyone is interested i moved the srand(time) declaration out of the function and this fixed everything.
    cheers

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, you only want to call srand once in the entire program, almost always at the very beginning. Otherwise srand often gets called over and over only milliseconds apart producing very bad random numbers. And you need code tags and proper tabbing when posting your code on here to make your code much more readable. Take a look at what you have above compared to this and see which you think is easier for us to read:
    Code:
    void CLine::RandomCoOrd()
    {
       SYSTEMTIME util;
       GetSystemTime(&util);
    
       m_XYCoOrds[0][0] = 2; //start point
       m_XYCoOrds[0][1] = 18;
       m_XYCoOrds[9][0] = 17;//end point
       m_XYCoOrds[9][1] = 5;
    
       m_DrawCoOrds[0][0] = 60;
       m_DrawCoOrds[0][1] = 60;
       m_DrawCoOrds[9][0] = 360;
       m_DrawCoOrds[9][1] = 320;
    
       int j = 0; // y axis counter
       //srand( util( NULL ) ); // initialize seed "randomly"
       srand(GetTickCount());
       for(int i=1; i<9; i++)
       {
    
          int r = rand() % 21; // generate a random number lower than 20
          m_XYCoOrds[i][j] = r;//set the random number as the line coords
          r = ((r*20)+20); //convert it to drawable coordinates
          m_DrawCoOrds[i][j] = r;
    
       }
    
       j = 1;
       for( i=1; i<9; i++)
       {
    
          int r = rand() % 21; // generate a random number lower than 20
          m_XYCoOrds[i][j] = r;//set the random number as the line coords
          r = (((20-r)*20)+20); //convert it to drawable coordinates
          m_DrawCoOrds[i][j] = r;
    
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 01:29 PM
  2. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 09:52 PM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 01:59 PM

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