Random number between 5-12

This is a discussion on Random number between 5-12 within the C++ Programming forums, part of the General Programming Boards category; So i want to have a code that returns a random number between 5 and 12 and i want to ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Random number between 5-12

    So i want to have a code that returns a random number between 5 and 12 and i want to have a difrent number every run.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Yea i allready tryed that...

    I tryed the one under:

    It's best to encapsulate this process in a function so that it can be called as many times as required.
    And i got this:
    ------ Build started: Project: Joost2, Configuration: Debug Win32 ------
    Compiling...
    Item1.cpp
    c:\documents and settings\joost van doorn\mijn documenten\visual studio 2005\projects\joost2\joost2\item1.cpp(31) : error C3861: 'time': identifier not found
    Build log was saved at "file://c:\Documents and Settings\Joost van Doorn\Mijn documenten\Visual Studio 2005\Projects\Joost2\Joost2\Debug\BuildLog.htm"
    Joost2 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Joost; 01-16-2006 at 08:24 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    : error C3861: 'time': identifier not found
    That error means you forgot to include ctime header file.
    Code:
    #include <ctime>
    ...
    ... // other stuff here

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >error C3861: 'time': identifier not found
    That program had a bug in that it didn't include time.h. That usually ends up working in C, but fails miserably in C++ because C++ requires a prototype.

  6. #6
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Genereer gewoon een random nummer en doe daar jouwn mimimun waarde bij, oftewel + 5. Om te zorgen dat je programma niet 2 keer dezelfde waarde genereerd moet je de waarde die je programma genereerd in bijvoorbeeld een integer zetten, kijk dan of je nieuwe gegenereerde nummer gelijk is aan een eerder gegenereerd nummer, zo ja genereer nog een keer tot dat het nieuwe gegenereede nummer niet gelijk is, als je hulp nodig hebt post ik mijn code wel even waarin dat gebeurd maar je kan het beter zelf doen want dan leer je er nog van, succes.
    Last edited by Yuri; 01-16-2006 at 08:38 AM.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Thank you for your help. I got a warning but it's working now

    How you know i'm dutch.

  8. #8
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Your name is very dutch, :P. Also "\mijn documenten". Anyway I will post a little program I made one day which uses the random funcion but will not use it twice:

    PHP Code:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    string S 256 ] [ 256 ],
           
    S2 256 ] [ 256 ],
           
    C;

    int i,
        
    j,
        
    k,
        
    256 ] [ 256 ],
        
    256 ] [ 256 ],
        
    256 ] [ 256 ],
        
    o,
        
    256 ] [ 256 ],
        
    NO 256 ] [ 256 ];

    int main()
    {

        
    cout<< "How many words?: ";
        
    cin>> C;

        if ( 
    isdigit ] ) &&
             
    isdigit ] ) || ] == '\0' &&
             
    isdigit ] ) || ] == '\0' )
        {

            
    atoi C.c_str() );

            if ( 
    || 100 )
            {

                
    system "cls" );
                
    cout<< "Wrong Input!";
                
    cin.ignore();
                
    cin.get();

                exit ( 
    );
            }
        }

        else
        {

            
    system "cls" );
            
    cout<< "Wrong Input!";
            
    cin.ignore();
            
    cin.get();

            exit ( 
    );
        }

        
    system "cls" );

        
    NO '\0' ] [ ] = 2;

        
    i++;

        for ( 
    1ij++ )
        {

            
    cout<< "Enter here the NL word: ";
            
    cin>> ] [ '\0' ];
            
    cout<< endl;

            
    cout<< "Enter here the OL word: ";
            
    cin>> '\0' ] [ ];
            
    cout<< endl;
        }

        
    i--;

        
    system "cls" );

        for ( 
    ] [ '\0' ] = 0'\0' ] [ ] = 0] [ '\0' ] = 0'\0' ] [ ] = 0] [ '\0' ] = 0'\0' ] [ ] = 0; ( ] [ '\0' ] + '\0' ] [ ] ) < ( ); )
        {

            
    srand ( ( unsigned time ) );

            if ( 
    NO '\0' ] [ ] == )
            {

                
    NO ] [ '\0' ] = 0;
            }

            else if ( 
    NO '\0' ] [ ] == )
            {

                
    NO ] [ '\0' ] = 1;
            }

            else if ( 
    NO '\0' ] [ ] == )
            {

                
    NO ] [ '\0' ] = rand() % 2;
            }

            if ( 
    NO ] [ '\0' ] == )
            {

                
    '\0';

                
    rand() % 1;

                for ( 
    1io++ )
                {

                    if ( 
    ] [ '\0' ] == )
                    {

                        
    0;

                        
    rand() % 1;
                    }
                }

                
    ] [ '\0' ]++;

                
    ] [ '\0' ] ] [ '\0' ] = j;

                
    ] [ '\0' ]++;

                if ( 
    ] [ '\0' ] >= )
                {

                    
    NO '\0' ] [ ] = 0;
                }
            }

            if ( 
    NO ] [ '\0' ] == )
            {

                
    '\0';

                
    rand() % 1;

                for ( 
    1io++ )
                {

                    if ( 
    '\0' ] [ ] == )
                    {

                        
    0;

                        
    rand() % 1;
                    }
                }

                
    '\0' ] [ ]++;

                
    '\0' ] [ '\0' ] [ ] ] = k;

                
    '\0' ] [ ]++;

                if ( 
    '\0' ] [ ] >= )
                {

                     
    NO '\0' ] [ ] = 1;
                }
            }

            
    cout<< ] [ ]<< " means: ";
            
    cin>> S2 ] [ ];
            
    cout<< endl;

            if ( 
    S2 ] [ ] == ] [ ] )
            {

                
    cout<< "Correct!";
                
    Sleep 1000 );

                
    p] [ '\0' ]++;
            }

            else
            {

                
    cout<< "Wrong!";
                
    Sleep 1000 );

                
    '\0' ] [ ]++;
            }

            
    system "cls" );
        }

        
    cout<< "Done!"<< endl<< endl;
        
    cout<< "You had "<< ] [ '\0' ]<< " correct awnsers and "<< '\0' ] [ ]<< " wrong awnsers."<< endl<< endl;
        
    cout<< "Your score would be "<< ( ( ] [ '\0' ] * 100 ) / ( ) )<< "% out of 100%";
        
    cin.ignore();
        
    cin.get();


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help regarding random number
    By Bargi in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 01:16 PM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  3. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 07:04 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM

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