Generate a random number

Discussion in 'C++' started by Acacia, Aug 25, 2003.

  1. Acacia

    Acacia Guest

    How would you generate a random number in C++?
     
    Acacia, Aug 25, 2003
    #1
    1. Advertising

  2. Acacia wrote:
    > How would you generate a random number in C++?
    >
    >


    The usual way: srand needs to be seeded with a random number and
    psuedo random numbers come out of rand().

    #include <iostream>
    #include <cstdlib>

    int main()
    {
    std::srand( 222 );

    std::cout << std::rand() << "\n";
    }


    If however you want cryptogrphically secure random number, you'll need
    to do some more work that is platform dependant.
     
    Gianni Mariani, Aug 25, 2003
    #2
    1. Advertising

  3. Acacia

    bartek d Guest

    "Acacia" <> wrote in news:bidtsq$t2a$1$8300dec7
    @news.demon.co.uk:

    > How would you generate a random number in C++?


    Please visit Bob Jenkins' page http://www.burtleburtle.net/bob/
    and see his work, esp. the "ISAAC" algorithm. It's also available as a C++
    template class. It's said to be cryptographically secure, fast, and
    unbiased.

    regards
    b
     
    bartek d, Aug 26, 2003
    #3
  4. Acacia

    Acacia Guest

    > #include <iostream>
    > #include <cstdlib>
    >
    > int main()
    > {
    > std::srand( 222 );
    >
    > std::cout << std::rand() << "\n";
    > }


    Upon compling (in MSVC) it returned the error:

    Compiling...
    c:\random.cpp
    c:\random.cpp(2) : fatal error C1083: Cannot open include file: 'cstdlib.h':
    No such file or directory

    CL returned error code 2.
    RANDOM.CPP - 1 error(s), 0 warning(s)



    that was after placing a '.h' after iostream and cstdlib. Before doing this
    returned this error:

    Compiling...
    c:\random.cpp
    c:\random.cpp(1) : fatal error C1083: Cannot open include file: 'iostream':
    No such file or directory

    CL returned error code 2.
    RANDOM.CPP - 1 error(s), 0 warning(s)


    There are seven errors and one warning when an attempt at compiling is made
    with the absence of the line '#include<cstdlib.h>'. These are:

    Compiling...
    c:\random.cpp
    c:\random.cpp(6) : error C2653: 'std' : is not a class name
    c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier
    c:\random.cpp(6) : error C2064: term does not evaluate to a function
    c:\random.cpp(8) : error C2653: 'std' : is not a class name
    c:\random.cpp(8) : error C2653: 'std' : is not a class name
    c:\random.cpp(8) : error C2065: 'rand' : undeclared identifier
    c:\random.cpp(8) : error C2064: term does not evaluate to a function
    c:\random.cpp(9) : warning C4508: 'main' : function should return a value;
    'void' return type assumed
    CL returned error code 2.
    RANDOM.CPP - 7 error(s), 1 warning(s)



    Could you please give me code for a working program that I can compile in
    either borland and/or msvc (v1.5) (note they are both old versions) that
    will generate three random numbers, to be placed in three different integers
    and then displayed to the screen. Thank you.
     
    Acacia, Aug 26, 2003
    #4
  5. Acacia

    Ron Natalie Guest

    "Acacia" <> wrote in message news:bige55$3bc$1$...

    > Could you please give me code for a working program that I can compile in
    > either borland and/or msvc (v1.5) (note they are both old versions) that
    > will generate three random numbers, to be placed in three different integers
    > and then displayed to the screen. Thank you.
    >

    VC++ 1.5? You've got to be kidding. That is ancient history. If you want
    to find out what might work in something that doesn't even know there is
    such a thing as standard C++, you should probably go to a group with
    microsoft in it's name, or at least read whatever passes for documetnation
    with those compilers. rand() or random() are the common names for those
    functions.
     
    Ron Natalie, Aug 26, 2003
    #5
  6. Acacia

    Bob Jacobs Guest

    [OT] Re: Generate a random number

    "Acacia" <> wrote in message
    news:bige55$3bc$1$...
    > > #include <iostream>
    > > #include <cstdlib>
    > >
    > > int main()
    > > {
    > > std::srand( 222 );
    > >
    > > std::cout << std::rand() << "\n";
    > > }

    >
    > Upon compling (in MSVC) it returned the error:
    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(2) : fatal error C1083: Cannot open include file:

    'cstdlib.h':
    > No such file or directory
    >
    > CL returned error code 2.
    > RANDOM.CPP - 1 error(s), 0 warning(s)
    >
    >
    >
    > that was after placing a '.h' after iostream and cstdlib. Before doing

    this
    > returned this error:
    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(1) : fatal error C1083: Cannot open include file:

    'iostream':
    > No such file or directory
    >
    > CL returned error code 2.
    > RANDOM.CPP - 1 error(s), 0 warning(s)
    >
    >
    > There are seven errors and one warning when an attempt at compiling is

    made
    > with the absence of the line '#include<cstdlib.h>'. These are:
    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(6) : error C2653: 'std' : is not a class name
    > c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier
    > c:\random.cpp(6) : error C2064: term does not evaluate to a function
    > c:\random.cpp(8) : error C2653: 'std' : is not a class name
    > c:\random.cpp(8) : error C2653: 'std' : is not a class name
    > c:\random.cpp(8) : error C2065: 'rand' : undeclared identifier
    > c:\random.cpp(8) : error C2064: term does not evaluate to a function
    > c:\random.cpp(9) : warning C4508: 'main' : function should return a value;
    > 'void' return type assumed
    > CL returned error code 2.
    > RANDOM.CPP - 7 error(s), 1 warning(s)
    >
    >
    >
    > Could you please give me code for a working program that I can compile in
    > either borland and/or msvc (v1.5) (note they are both old versions) that
    > will generate three random numbers, to be placed in three different

    integers
    > and then displayed to the screen. Thank you.


    Unfortunately you're using a very old compiler, so you'll need to use
    pre-standard headers. Something along the following lines should work (but I
    don't have VC++ 1.5 to test it with - if not, consult the VC++ help files
    and look up rand and srand).

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    srand((unsigned) time(NULL));
    int a = rand();
    int b = rand();
    int c = rand();
    cout << a << " " << b << " " << c << endl;
    return 0;
    }

    As this group discusses standard C++, posts that are specific to VC++ are
    considered off-topic so you'll need to ask further questions in a newsgroup
    that discusses VC++, or perhaps better, search Google for C++ forums and try
    some of the sites listed - some of those may be more tolerant of questions
    for such an old compiler.

    Also, be aware that C++ has changed somewhat since VC++ 1.5 was current, so
    you'd be well advised to junk it in favour of a more up to date compiler
    and, presumably, an up to date book. Free compilers are available for some
    platforms. The FAQ may prove useful to you here:

    http://www.parashift.com/c -faq-lite/

    HTH
     
    Bob Jacobs, Aug 26, 2003
    #6
  7. Acacia wrote:

    >>#include <iostream>
    >>#include <cstdlib>
    >>
    >>int main()
    >>{
    >> std::srand( 222 );
    >>
    >> std::cout << std::rand() << "\n";
    >>}

    >
    >
    > Upon compling (in MSVC) it returned the error:
    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(2) : fatal error C1083: Cannot open include file: 'cstdlib.h':
    > No such file or directory
    >


    There is no such standard header. No surprise here.

    > CL returned error code 2.
    > RANDOM.CPP - 1 error(s), 0 warning(s)
    >
    >
    >
    > that was after placing a '.h' after iostream and cstdlib. Before doing this
    > returned this error:


    Then you introduced errors where there were none. The code above has the
    correct headers specified.

    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(1) : fatal error C1083: Cannot open include file: 'iostream':
    > No such file or directory


    Then the compiler is either broken, or very, very old.

    >
    > CL returned error code 2.
    > RANDOM.CPP - 1 error(s), 0 warning(s)
    >
    >
    > There are seven errors and one warning when an attempt at compiling is made
    > with the absence of the line '#include<cstdlib.h>'. These are:
    >
    > Compiling...
    > c:\random.cpp
    > c:\random.cpp(6) : error C2653: 'std' : is not a class name


    No, it's not. It's a namespace name. But the compiler wouldn't see that
    if the only header included was the non-standard <iostream.h>.

    > c:\random.cpp(6) : error C2065: 'srand' : undeclared identifier


    Yes, because srand is declared in <cstdlib>.

    > c:\random.cpp(6) : error C2064: term does not evaluate to a function
    > c:\random.cpp(8) : error C2653: 'std' : is not a class name
    > c:\random.cpp(8) : error C2653: 'std' : is not a class name
    > c:\random.cpp(8) : error C2065: 'rand' : undeclared identifier


    Also in <cstdlib>.

    > c:\random.cpp(8) : error C2064: term does not evaluate to a function
    > c:\random.cpp(9) : warning C4508: 'main' : function should return a value;
    > 'void' return type assumed


    This is an error in the compiler. Main implicitly returns 0 if it
    reaches the end without finding a return statement. Making main return
    void makes it illegal.

    > CL returned error code 2.
    > RANDOM.CPP - 7 error(s), 1 warning(s)
    >
    >
    >
    > Could you please give me code for a working program that I can compile in
    > either borland and/or msvc (v1.5) (note they are both old versions) that
    > will generate three random numbers, to be placed in three different integers
    > and then displayed to the screen. Thank you.


    Since those compilers are too old to support standard C++, any such code
    would not really be topical here. But here's an adaptation of the code
    above for old, pre-standard compilers:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    srand( 222 );

    cout << rand() << "\n";
    return 0;
    }

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 26, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    24
    Views:
    9,447
    Malcolm
    Oct 18, 2004
  2. RayKnight

    Generate a random number

    RayKnight, Apr 29, 2005, in forum: Java
    Replies:
    2
    Views:
    344
    Peter MacMillan
    Apr 29, 2005
  3. Wahoo
    Replies:
    4
    Views:
    477
    Jerry Coffin
    May 18, 2004
  4. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,313
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page