Thread: Dev C++ |Random Coding| Not Working!

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    97

    Dev C++ |Random Coding| Not Working!

    i am now trying to make random values. here is my simple lil program to learn more about it. it has a error when i compile tho.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    const int LOW = 1;
    const int HIGH = 6;
    
    int main()
    {
    int firstdie;
    int secdie;
    
    firstdie = rand() % (HIGH - LOW);
    secdie = rand() % (HIGH - LOW);
    
    cout << "You rolled a " << firstdie;
    cout << "\n\nAnd you rolled a " << secdie;
    system("pause");
    }
    the compiling errors are:

    C:\My Documents\C++ Stuff\Un-Important Working Programs\random.cpp
    [Warning] In function `int main()':

    15 C:\My Documents\C++ Stuff\Un-Important Working Programs\random.cpp
    `cout' undeclared (first use this function)

    15 C:\My Documents\C++ Stuff\Un-Important Working Programs\random.cpp
    (Each undeclared identifier is reported only once for each function it appears


    thx alot!

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Add after includes:
    Code:
    using namespace std;
    or even better:
    Code:
    using std::cout;
    using std::rand;
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    cant beleive i forgot that! thx so much man! check ur game thread too, i replied in it. but now, each time it is run, i roll a 1 and a 2 each time. im wondering if its that seeding the random value thing i have to do(which i dont understand and need help with), or just what i rolled. u know? thx

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Have to use srand() to seed the timer

    use the following at the beginning of the program and only execute it once to seed the random number genrator with the system time

    Code:
    srand ( (unsigned)time(NULL) );
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    execute it once? like compile the prog with that code in it(before int main() right?), then delte the code after? what do u exactly mean by execute the program only once? i might distribute the program or sumthin!

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    He means, do NOT place srand in a loop or a recursive function. This is because if you have your seed set to
    the clock and put in a loop, as you
    know a loop takes much less than a second, so it will end up turning up the same thing till a second has
    passed (assuming this is how you seed rand):

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main( void )
    {
      int Number;
      srand( time( 0 ) ); //As I do it...
      Number = rand % ( 12 - 6 + 1 ) + 6; //This will generate a number between 12 and 6, substitue 12   
      std::cout << "You random number is: " << Number; //for your high number and 6 for your low...
      return 0;
    }
    An example of a bad way to use srand:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main( void )
    {
      int Number;
      for( int C = 0; C < 10; C++ )
      {
        srand( time( 0 ) );
        Number = rand % ( HIGH - LOW + 1 ) + LOW;
        std::cout << Number << ' ';
      }
      return 0;
    }
    Since the seed is placed in the loop and it is using seconds to seed rand, the loop will happen several times
    a second, producing the same number several times. Say HIGH is 5, and LOW is 1:

    1111444422...

    That is an example, it will use the same number several times before switching to another number.

    - SirCrono6
    Last edited by SirCrono6; 03-05-2004 at 10:05 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    wow, thx for the examale. lemme go try it, brb.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. DEV C++ Limitations?
    By Kirdra in forum Game Programming
    Replies: 3
    Last Post: 09-09-2002, 09:40 PM
  3. random in Dev C++ still not working
    By akmalsafi in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2001, 03:54 PM
  4. use of "random" in Dev C++
    By akmalsafi in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2001, 02:55 AM
  5. Tutorial about Bloodshed Dev!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2001, 07:42 PM