alittle help please

This is a discussion on alittle help please within the C++ Programming forums, part of the General Programming Boards category; hi there! can anyone tell me how to write a program to produce a random number? please go easy on ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    59

    alittle help please

    hi there!

    can anyone tell me how to write a program to produce a random number?

    please go easy on me im only a newbie at this c++ thing!

    cheers in advance

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: alittle help please

    Originally posted by boontune
    hi there!

    can anyone tell me how to write a program to produce a random number?

    please go easy on me im only a newbie at this c++ thing!

    cheers in advance
    Pretty simple.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int main( void )
    {
      // Seed the random number generator
      srand( time(NULL) );
    
      cout << "Random number: " << rand() % 255 << endl;
    
      return 0;
    }
    You seed the random number generator with the current time so you get different numbers each time it is run. The reason I modded the function rand() with 255 is to reduce the range of randomness.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    thanks alot mate

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    And new to the Boards, as well.

    Top of your screen, right-hand side. Click on the "search" button and have a go.

    This is one of those questions that's been discussed many, many times before.

    Read the FAQ, as well. It will put you in good stead with those with itchy trigger fingers, if you know what I mean.

    (MrWizard's assistance aside, if I hadn't said something to you about this, somebody else would have, and I'm, typically, more gentle about these things than some others are.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alittle help for a beginner please...
    By Xeavor in forum C++ Programming
    Replies: 23
    Last Post: 09-24-2004, 03:10 AM
  2. Noobie <-- alittle help /w compiler
    By Bean0578 in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 10:22 PM
  3. Alittle help please
    By boontune in forum C++ Programming
    Replies: 13
    Last Post: 12-10-2002, 04:41 PM
  4. need alittle help
    By xlordt in forum Linux Programming
    Replies: 1
    Last Post: 02-14-2002, 11:53 AM
  5. Please alittle help-- if/else
    By jill in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 07:13 PM

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