Thread: A question about C++ random generator and unsigned method

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    A question about C++ random generator and unsigned method

    My teacher gave me those code from random.cpp
    But i have a question about how to code a Random() constructor in random.h

    also, i feel really confuse how to code a Random( unsigned int newSeed ) in random.h too

    and what does the "unsigned" for?

    Thanks for help me, i would really appericate it.



    Code:
    // System Include Files
    #include <cstdlib>     // Required for RAND_MAX
    #include <iostream>
    
    // User Include Files
    #include "random.h"
    
    // Namespaces
    using namespace std;
    
    // Other Preprocessor Commands
    // User Defined Types
    // Function Prototypes
    // Global Variables - Don't Use!
    
    
    int main( void )
    {
        Random rand;
    
        int iCtr;
    
        cout << "Generate 25 random numbers between 0 and 99..." << endl;
        for ( iCtr = 0; iCtr < 25; iCtr++ )
        {
            cout.width( 3 );
            cout << rand.getRandomNumber( 0, 99 ) << " ";
        }
        cout << endl << endl << endl;
    
        cout << "Generate 15 random numbers between 57 and 189..." << endl;
        for ( iCtr = 0; iCtr < 15; iCtr++ )
        {
            cout.width( 4 );
            cout << rand.getRandomNumber( 57, 189 ) << " ";
        }
        cout << endl << endl;
    
        unsigned int randNo;
    
        cout << "Testing 10000 random numbers between 5 and 156..." << endl;
        for ( iCtr = 0; iCtr < 10000; iCtr++ )
        {
            randNo = rand.getRandomNumber( 5, 156 );
            if ( ( randNo < 5 ) || ( randNo > 156 ) )
            {
                cerr << "ERROR: " << randNo << endl;
            }
    
            if ( ( randNo == 5 ) || ( randNo == 156 ) )
            {
                cout << "Boundry: " << randNo << endl;
            }
        }
    
        return EXIT_SUCCESS;
    }
    
    
    // End of file: RANDOMTEST.CPP

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your default constructor for the random class should simply call srand using the return value from the time function as input. Your other constructor, the one that takes an unsigned int as an argument, should also call srand but it should pass the unsigned int argument to srand instead of srand initializing itself via the time call. I'm guessing that is what would be required.

    Integer and character data types can be either signed or unsigned. A signed int can have values from (assuming 32-bit integers) -2,147,483,648 up to +2,147,483,647. An unsigned int does away with caring about negative values and deals only with the positive meaning that it can hold values from 0 up to +4,294,967,295. By default, if you don't specify signed or unsigned, the compiler will assume signed:

    Code:
    int val;           // Signed integer value
    signed int val;    // Same as above, signed integer value
    unsigned int val;  // Unsigned integer value;
    Last edited by hk_mp5kpdw; 03-14-2005 at 06:59 AM. Reason: Off by one on the unsigned int max value
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    thanks

    can you give me some examples how to what i should type inside the random constructor?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The FAQ has examples of initializing srand using the time function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    can you show me how to call a srand and how to return time in random constructor?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >can you show me how to call a srand and how to return time in random constructor?
    You don't need to return anything, just call it. This will seed the random number generator.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    so This is what i did in my random.h file
    Code:
    #ifndef RANDOM_H
    #define RANDOM_H
    #include <cstdlib>
    
    class Random
    {
    public:
    	Random()
    	{
    		int i;
    		srand(time(NULL));
    	}
    is it like that?
    However i need to create one more constructor for Random( unsigned int newSeed)

    What should i type inside this constructor??

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by joenching
    so This is what i did in my random.h file

    Code:
    #ifndef RANDOM_H
    #define RANDOM_H
    #include <cstdlib>
    
    class Random
    {
    public:
        Random()
        {
            int i;
            srand(time(NULL));
        }
    is it like that?
    The part in red above is unnecessary, but other than that, yes.

    Quote Originally Posted by joenching
    However i need to create one more constructor for Random( unsigned int newSeed)

    What should i type inside this constructor??
    Quote Originally Posted by me
    Your other constructor, the one that takes an unsigned int as an argument, should also call srand but it should pass the unsigned int argument to srand instead of srand initializing itself via the time call.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    is it like that? the second constactor?
    Code:
    Random( unsigned int newSeed)
    	{
    		srand(newSeed);	
    	}
    Also I have one question about the accessors
    I am required to code a function "unsigned int getSeed() const" inside the public

    But what should i type for the return type?

    [code]
    unsigned int getSeed()
    { return ??; }
    [code/]

    And Do i need to create an attribute inside private?

    thanks
    Last edited by joenching; 03-14-2005 at 03:10 PM.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by joenching
    is it like that? the second constactor?
    Code:
    Random( unsigned int newSeed)
    	{
    		srand(newSeed);	
    	}
    Looks good to me!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Also I have one question about the accessors
    I am required to code a function "unsigned int getSeed() const" inside the public

    But what should i type for the return type?

    Code:
    unsigned int getSeed()
    { return ??; }
    And Do i need to create an attribute inside private?

    thanks

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It depends on what getSeed() is supposed to do. If it's supposed to automatically generate a seed, then you could call time.
    Code:
    unsigned int getSeed()
    { return time(NULL); }
    If it's supposed to ask the user for a seed, then you could use cin to get input from the user, then return that input as the seed.

    >And Do i need to create an attribute inside private?
    I don't think so, but I may be misinterpreting what getSeed() does.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Hey, this is my header file Randon.h

    Code:
    #ifndef RANDOM_H
    #define RANDOM_H
    #include <cstdlib>
    #include <ctime>
    class Random
    {
    public:
    	Random()
    	{
    		srand(time(NULL));
    	}
    
    	Random( unsigned int newSeed)
    	{
    		srand(newSeed);	
    	}
    		
    	
    	int getRandomNumber( int min = 0, 
    						 int max = RAND_MAX ) const
    	{
    		return time(NULL);
    	}
    	
    		
    		
    //	bool operator==(const Random& rhRandom) const
    
    //	bool operator!=(const Random& rhRandom) const
    
    //	unsigned int getRandomNumber( unsigned min = 0,
    //								  unsigned max = RAND_MAX ) const
    
    //	unsigned int getSeed() const
    
    //	void setSeed( unsigned int newSeed)
    //	{
    //		number = newSeed;
    //	}
    
    private:
    
    	int number;
    
    };
    
    #endif



    and this is my cpp file for Random.cpp

    Code:
    int main( void )
    {
        Random rand;
    
        int iCtr;
    
        cout << "Generate 25 random numbers between 0 and 99..." << endl;
        for ( iCtr = 0; iCtr < 25; iCtr++ )
        {
            cout.width( 3 );
            cout << rand.getRandomNumber( 0, 99 ) << " ";
        }
        cout << endl << endl << endl;
    
        cout << "Generate 15 random numbers between 57 and 189..." << endl;
        for ( iCtr = 0; iCtr < 15; iCtr++ )
        {
            cout.width( 4 );
            cout << rand.getRandomNumber( 57, 189 ) << " ";
        }
        cout << endl << endl;
    I tried to compile it, it works.. However it shows me a terrible result is like that..

    Generate 25 random numbers between 0 and 99...
    1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 111
    0836425 1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 111083
    6425 1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 111083642
    5 1110836425 1110836425 1110836425


    Generate 15 random numbers between 57 and 189...
    1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 111
    0836425 1110836425 1110836425 1110836425 1110836425 1110836425 1110836425 111083
    6425

    Press any key to continue

    Do you know what's wrong is it??

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    getRandomNumber() should call rand() to get a random number.

    By the way, judging from your code, getSeed() would probably look like this instead of what I posted above:
    Code:
    unsigned int getSeed()
    { return number; }

Popular pages Recent additions subscribe to a feed