Random Numbers

Discussion in 'C++' started by Wilson, Feb 8, 2007.

  1. Wilson

    Wilson Guest

    I am new to c++ and part of my program needs to create three random
    numbers and then output them. I decided to use the computer clock to
    help ouput the numbers but i constantly get the same results. I have
    used the header file cstdlib for functions rand and srand. Can you
    please advise how to alter the program to create random numbers every
    time the program is run (or suggest alternate ways)

    thanks
    Wilson, Feb 8, 2007
    #1
    1. Advertising

  2. Wilson

    Ian Collins Guest

    Wilson wrote:
    > I am new to c++ and part of my program needs to create three random
    > numbers and then output them. I decided to use the computer clock to
    > help ouput the numbers but i constantly get the same results. I have
    > used the header file cstdlib for functions rand and srand. Can you
    > please advise how to alter the program to create random numbers every
    > time the program is run (or suggest alternate ways)
    >

    What program?

    Post the relevant code so we can see what you have and how to improve
    it. rand() will give the same sequence for the same seed, so you must be
    seeding it incorrectly.

    --
    Ian Collins.
    Ian Collins, Feb 8, 2007
    #2
    1. Advertising

  3. Wilson

    Tim Slattery Guest

    "Wilson" <> wrote:

    >I am new to c++ and part of my program needs to create three random
    >numbers and then output them. I decided to use the computer clock to
    >help ouput the numbers but i constantly get the same results. I have
    >used the header file cstdlib for functions rand and srand. Can you
    >please advise how to alter the program to create random numbers every
    >time the program is run (or suggest alternate ways)


    srand((unsigned int)time(NULL));

    printf("%d\n", rand());
    printf("%d\n", rand());

    --
    Tim Slattery

    http://members.cox.net/slatteryt
    Tim Slattery, Feb 8, 2007
    #3
  4. Wilson

    Jim Langston Guest

    "Wilson" <> wrote in message
    news:...
    >I am new to c++ and part of my program needs to create three random
    > numbers and then output them. I decided to use the computer clock to
    > help ouput the numbers but i constantly get the same results. I have
    > used the header file cstdlib for functions rand and srand. Can you
    > please advise how to alter the program to create random numbers every
    > time the program is run (or suggest alternate ways)
    >
    > thanks


    This may work for you. Not sure if time() is os dependant.

    srand( time() );

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

    I think libraries are
    #include <time.h>
    #include <stdlib>

    You might be able to use <ctime>
    not sure.
    Jim Langston, Feb 8, 2007
    #4
  5. Jim Langston wrote:
    > "Wilson" <> wrote in message
    > news:...
    >> I am new to c++ and part of my program needs to create three random
    >> numbers and then output them. I decided to use the computer clock to
    >> help ouput the numbers but i constantly get the same results. I have
    >> used the header file cstdlib for functions rand and srand. Can you
    >> please advise how to alter the program to create random numbers every
    >> time the program is run (or suggest alternate ways)
    >>
    >> thanks

    >
    > This may work for you. Not sure if time() is os dependant.
    >
    > srand( time() );


    'time()' is not OS-dependant, but the essence of its return value type
    is implementation-specific. Since 'srand' expects 'unsigned', a cast
    is an order:

    srand ( (unsigned) time() );

    Whether it achieves the expected result is implementation-defined, as
    well. For all we know, 'time_t' can be such that casting it to
    an 'unsigned int' value will always result in, say, 0, which would
    defeat the purpose of using the return value of 'time()' as the seed.
    IOW, YMMV, but in most cases it works.

    >
    > std::cout << rand() << "\n";
    >
    > I think libraries are
    > #include <time.h>
    > #include <stdlib>


    Those are "headers", not "libraries". And it's not <stdlib>, it's
    <stdlib.h>.

    > You might be able to use <ctime>
    > not sure.


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 8, 2007
    #5
  6. On Feb 8, 11:05 pm, "Victor Bazarov" <> wrote:
    > Jim Langston wrote:
    > > "Wilson" <> wrote in message
    > >news:...
    > >> I am new to c++ and part of my program needs to create three random
    > >> numbers and then output them. I decided to use the computer clock to
    > >> help ouput the numbers but i constantly get the same results. I have
    > >> used the header file cstdlib for functions rand and srand. Can you
    > >> please advise how to alter the program to create random numbers every
    > >> time the program is run (or suggest alternate ways)

    >
    > >> thanks

    >
    > > This may work for you. Not sure if time() is os dependant.

    >
    > > srand( time() );

    >
    > 'time()' is not OS-dependant, but the essence of its return value type
    > is implementation-specific. Since 'srand' expects 'unsigned', a cast
    > is an order:
    >
    > srand ( (unsigned) time() );
    >
    > Whether it achieves the expected result is implementation-defined, as
    > well. For all we know, 'time_t' can be such that casting it to
    > an 'unsigned int' value will always result in, say, 0, which would
    > defeat the purpose of using the return value of 'time()' as the seed.
    > IOW, YMMV, but in most cases it works.
    >
    >
    >
    > > std::cout << rand() << "\n";

    >
    > > I think libraries are
    > > #include <time.h>
    > > #include <stdlib>

    >
    > Those are "headers", not "libraries". And it's not <stdlib>, it's
    > <stdlib.h>.


    <ctime> and <cstdlib> if you want to be correct.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 9, 2007
    #6
  7. Erik Wikström wrote:
    > On Feb 8, 11:05 pm, "Victor Bazarov" <> wrote:
    >> Jim Langston wrote:
    >>> I think libraries are
    >>> #include <time.h>
    >>> #include <stdlib>

    >>
    >> Those are "headers", not "libraries". And it's not <stdlib>, it's
    >> <stdlib.h>.

    >
    > <ctime> and <cstdlib> if you want to be correct.


    Just so that newbies don't get the wrong impression... It's not
    "incorrect" to use the .h form of C headers.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 9, 2007
    #7
  8. Wilson

    Pete Becker Guest

    Erik Wikström wrote:
    >>> I think libraries are
    >>> #include <time.h>
    >>> #include <stdlib>

    >> Those are "headers", not "libraries". And it's not <stdlib>, it's
    >> <stdlib.h>.

    >
    > <ctime> and <cstdlib> if you want to be correct.
    >


    All of <ctime>, <time.h>, <cstdlib>, and <stdlib.h> are correct.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Feb 9, 2007
    #8
    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. globalrev
    Replies:
    4
    Views:
    753
    Gabriel Genellina
    Apr 20, 2008
  2. Alex Untitled
    Replies:
    11
    Views:
    657
    Giampiero Zanchi
    Nov 16, 2009
  3. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Feb 12, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    216
    PerlFAQ Server
    Feb 12, 2011
  4. PerlFAQ Server

    FAQ 4.10 Why aren't my random numbers random?

    PerlFAQ Server, Apr 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    249
    PerlFAQ Server
    Apr 27, 2011
  5. VK
    Replies:
    15
    Views:
    1,152
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page