Hi all,can enyone give a solution ,how to emplement random function,so it could randomly select numbers from the saved text file(grid) and output in matrix . What i try to do is:Create animation project,but would like color matrix as a background.What i did so far is came up to the idea using matrix as a background,now i need create an image in .....dat file and implement random function,so then i run the program this image would be in the matrix,color.So is the random function can do it? Any ideas are welcome.Regards.

Recommended Answers

All 2 Replies

Hard to tell for sure, but it seems to me that you want to randomize the background color. Simply make an array of the color choices and get a random index to choose the color. If you need help with using the rand() function here's a tutorial on it.

For random colors I use these two functions:

int RandomInt (const int Min, const int Max) 
/*
PURPOSE: To return a random number within or on the range handed
to us here.
*/
{
    int Num,Delta,RandNum ;

    RandNum = rand() ;
    /*
     * Put RandNum is between 0 and RAND_MAX-1 inclusive
     */
    if (RandNum == RAND_MAX) {
        RandNum = RAND_MAX - 1 ;
    }

    /*
     * Cover the correct magnitude. The interval is RAND_MAX/Delta
     * and so to cover a delta of 1 integer we add 1. Draw the
     * diagram!
     */
    Delta =  ((Max - Min) + 1) ;
    Num   = (RandNum * Delta) / RAND_MAX ;

    // Get Num to cover the correct range
    Num = Num + Min ;

    // Return the random int
    return (Num) ;
}

/*********************************************************************/

...and...

COLORREF RandomCol (void)
/*
PURPOSE: Create a random 24 bit colour
*/
{
    BYTE Red,Green,Blue ;

    Red   = (BYTE)(RandomInt (0,255)) ;
    Green = (BYTE)(RandomInt (0,255)) ;
    Blue  = (BYTE)(RandomInt (0,255)) ;
    return (RGB(Red,Green,Blue)) ;
}

The core is the call to rand()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.