Thread: random numbers

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    22

    random numbers

    I had a look at the random numbers tutorial and was wondering why doesn't this work???

    Code:
    #include <iostream>
    #include <stdlib>
    #include <time>
    /*
    These constants define our upper
    and our lower bounds. The random numbers
    will always be between 1 and 6, inclusive.
    */
    const int LOW = 1;
    const int HIGH = 3;
    int main()
    {
    /*
    Variables to hold random values
    for the first and the second die on
    each roll.
    */
    int first_die, sec_die;
    /*Ê
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Get first and second random numbers.
    */
    first_die = rand() % (HIGH - LOW + 1) + LOW;
    sec_die = rand() % (HIGH - LOW + 1) + LOW;
    /*
    Output first roll results.
    */
    cout<<"I chose the mighty\n";
    if ( first_die == 1 ) {
       cout<<"Paper!\n";
    }
    else if ( first_die == 2 ) {
         cout<<"Rock!\n";
         }
    else {
         cout<<"Scisors!\n";
         }
         
    cout<<"I like the number "<< sec_die << "}" << endl << endl;
    cin.get();
    /*
    Get two new random values.
    */
    first_die = rand() % (HIGH - LOW + 1) + LOW;
    sec_die = rand() % (HIGH - LOW + 1) + LOW;
    /*
    Output second roll results.
    */
    cout<< "My roll is (" << first_die << ", "
    << sec_die << "}" << endl << endl;
    cin.get();
    }
    It says thet cout is unidentified, why's this?

    Thanks

  2. #2
    Hello,

    For a simple answer and example, view the following link: C++ Tutorial: 5.2, Namespaces

    Scroll down until you find the Namespace std sub-section. It should answer your question.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Ok, thanks.

    --edit--

    Oh my god! How the heck did I not put using namspace std??? That's the first time I've ever done that! Also I copied that off this website, I think the admin should have a look at the rand numbers tutorial.
    Last edited by ballmonkey; 01-18-2005 at 01:51 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I think the admin should have a look at the rand numbers tutorial.
    There are actually a lot of problems with the tutorials, but it's a tough job to fix all the mistakes. The administrators all have full-time jobs, and they put in a lot of work maintaining the site as it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM