Thread: random number help

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    random number help

    I am trying to simulate the rolling of a six sided die ten time...but get a compile error.

    Code:
    #include <cstdlib>
    using std::rand;
    using std::srand;
    
    #include <iostream>
    using std::cout;
    using std::endl;
    
    #include <ctime>
    using std::time;
    
    
    int main()
    {
        const int arraysize = 7;
        int value[arraysize] = {0};
        
        srand( time( 0 ) ); // seed random number generator
        for( int roll = 1, roll <=10, roll++) // roll die 10 times
        value[ 1 + rand() % 6]++;
        cout << value << endl;
        
        system("PAUSE");
        return 0;
    }

    Errors:
    22 C:\Dev-Cpp\main.cpp expected init-declarator before '<=' token
    22 C:\Dev-Cpp\main.cpp expected `,' or `;' before '<=' token
    26 C:\Dev-Cpp\main.cpp expected `)' before ';' token

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Take a look at the second error message and you'll get there.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    yeah...got it

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    array indexes are zero based
    0,1,...6 are valid indexes, so why are you using 1..6?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM