How to use srand and rand? - c++

This is a discussion on How to use srand and rand? - c++ ; How to use srand and rand? I tried : cout << srand(2,12); cout << srand(12); cout << rand(12); these does not work.What i need is to get a random number from specified range of numbers min and max.from 6 to ...

+ Reply to Thread
Results 1 to 7 of 7

How to use srand and rand?

  1. Default How to use srand and rand?

    How to use srand and rand?

    I tried :

    cout << srand(2,12);

    cout << srand(12);

    cout << rand(12);

    these does not work.What i need is to get a random number from
    specified range of numbers min and max.from 6 to 12 for example.How to
    do that?

    And what is the difference between these two commands srand and rand?


  2. Default Re: How to use srand and rand?

    PencoOdStip wrote:
    > How to use srand and rand?
    >
    > I tried :
    >
    > cout << srand(2,12);
    >
    > cout << srand(12);
    >
    > cout << rand(12);
    >
    > these does not work.What i need is to get a random number from
    > specified range of numbers min and max.from 6 to 12 for example.How to
    > do that?
    >
    > And what is the difference between these two commands srand and rand?
    >

    Jeebus, dude. If you can't get any of this stuff from your book,
    perhaps try to find one translated to your native language, or another
    one that fits your learning style.

    Anyways... you have the wrong idea about srand(). Its purpose is to
    'seed' the random number generator. Using a seed will cause the random
    number generator to produce a certain, fixed set of 'random' numbers.

    This will answer your question a little better:
    http://www.cplusplus.com/reference/c...dlib/rand.html

    Hint for future questions like this: It was the first page that came up
    when I did a google search.

  3. Default Re: How to use srand and rand?

    "shadowman" writes:

    > PencoOdStip wrote:
    >> How to use srand and rand?
    >>
    >> I tried :
    >>
    >> cout << srand(2,12);
    >>
    >> cout << srand(12);
    >>
    >> cout << rand(12);
    >>
    >> these does not work.What i need is to get a random number from
    >> specified range of numbers min and max.from 6 to 12 for example.How to
    >> do that?
    >>
    >> And what is the difference between these two commands srand and rand?
    >>

    > Jeebus, dude. If you can't get any of this stuff from your book, perhaps
    > try to find one translated to your native language, or another one that
    > fits your learning style.
    >
    > Anyways... you have the wrong idea about srand(). Its purpose is to
    > 'seed' the random number generator. Using a seed will cause the random
    > number generator to produce a certain, fixed set of 'random' numbers.
    >
    > This will answer your question a little better:
    > http://www.cplusplus.com/reference/c...dlib/rand.html
    >
    > Hint for future questions like this: It was the first page that came up
    > when I did a google search.


    I agree with everything Shadowman said. This is not a newsgroup formed so
    that someone could ask 1,000 questions and in the end be a programmer. It
    is meant for occasional questions where a book or instructor has not left
    something to be desired in the way of clarity. A few more of these primer
    level questions and you, pencoodstip, are going to be the sole occupant of
    my E-mail filter. Assuming I can find it and figure out how to use it.



  4. Default Re: How to use srand and rand?

    PencoOdStip writes:
    > How to use srand and rand?
    >
    > I tried :
    >
    > cout << srand(2,12);
    >
    > cout << srand(12);
    >
    > cout << rand(12);
    >
    > these does not work.What i need is to get a random number from
    > specified range of numbers min and max.from 6 to 12 for example.How to
    > do that?
    >
    > And what is the difference between these two commands srand and rand?


    You will find the answers to these and many other questions in your
    book, or in your system's documentation, or via a simple web search.

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

  5. Default Re: How to use srand and rand?

    <PencoOdStip> wrote in message
    news:1180118509.482625.312040@g4g2000hsf.googlegroups.com...
    > How to use srand and rand?
    >
    > I tried :
    >
    > cout << srand(2,12);
    >
    > cout << srand(12);
    >
    > cout << rand(12);
    >
    > these does not work.What i need is to get a random number from
    > specified range of numbers min and max.from 6 to 12 for example.How to
    > do that?
    >
    > And what is the difference between these two commands srand and rand?


    srand is to seed the random number generator. I generally use it like:

    srand( clock() );

    rand() returns a pseudo-random number from 0 to MAXRAND ( or MAX_RAND or
    something like that) which is 32767 on my ssytem.

    Read your documentation on them.



  6. Default Re: How to use srand and rand?

    On 2007-05-25 20:47:58 -0700, "Jim Langston" <tazmaster@rocketmail.com> said:

    > <PencoOdStip> wrote in message
    > news:1180118509.482625.312040@g4g2000hsf.googlegroups.com...
    >> How to use srand and rand?
    >>
    >> I tried :
    >>
    >> cout << srand(2,12);
    >>
    >> cout << srand(12);
    >>
    >> cout << rand(12);
    >>
    >> these does not work.What i need is to get a random number from
    >> specified range of numbers min and max.from 6 to 12 for example.How to
    >> do that?
    >>
    >> And what is the difference between these two commands srand and rand?

    >
    > srand is to seed the random number generator. I generally use it like:
    >
    > srand( clock() );


    Using clock() is a very bad idea here. Use time() instead. clock() is
    very likely to return the same value on different runs of your program,
    therefore leading you to use the same sequence of pseudo-random numbers
    multiple times.

    > rand() returns a pseudo-random number from 0 to MAXRAND ( or MAX_RAND or
    > something like that) which is 32767 on my ssytem.
    >
    > Read your documentation on them.



    --
    Clark S. Cox III
    clarkcox3


  7. Default Re: How to use srand and rand?

    "Clark Cox" <clarkcox3> wrote in message
    news:2007052610391575249-clarkcox3@gmailcom...
    > On 2007-05-25 20:47:58 -0700, "Jim Langston" <tazmaster@rocketmail.com>
    > said:
    >
    >> <PencoOdStip> wrote in message
    >> news:1180118509.482625.312040@g4g2000hsf.googlegroups.com...
    >>> How to use srand and rand?
    >>>
    >>> I tried :
    >>>
    >>> cout << srand(2,12);
    >>>
    >>> cout << srand(12);
    >>>
    >>> cout << rand(12);
    >>>
    >>> these does not work.What i need is to get a random number from
    >>> specified range of numbers min and max.from 6 to 12 for example.How to
    >>> do that?
    >>>
    >>> And what is the difference between these two commands srand and rand?

    >>
    >> srand is to seed the random number generator. I generally use it like:
    >>
    >> srand( clock() );

    >
    > Using clock() is a very bad idea here. Use time() instead. clock() is very
    > likely to return the same value on different runs of your program,
    > therefore leading you to use the same sequence of pseudo-random numbers
    > multiple times.


    Hmm, yes, you seem to be right. Clock returns the time since the program
    began so, yes, it could very possibly contain the same number on subsequent
    runs of the program. I have never really looked at the output of clock but
    derivations of it (starttime = clock(); endtime = clock(); std::cout <<
    endtime - startime so never even though about it. Bad me.

    I'll have to remember to use time() in the future (not using srand in any
    programs in production so don't have to fix them).

    >> rand() returns a pseudo-random number from 0 to MAXRAND ( or MAX_RAND or
    >> something like that) which is 32767 on my ssytem.
    >>
    >> Read your documentation on them.




+ Reply to Thread

Similar Threads

  1. Re: rand()
    By Application Development in forum TCL
    Replies: 4
    Last Post: 10-03-2007, 11:12 AM
  2. Re: rand()
    By Application Development in forum TCL
    Replies: 0
    Last Post: 10-03-2007, 09:31 AM
  3. Re: rand()
    By Application Development in forum TCL
    Replies: 2
    Last Post: 10-02-2007, 01:05 PM
  4. rand() v. rand(0.1) ?
    By Application Development in forum RUBY
    Replies: 6
    Last Post: 09-16-2007, 10:18 AM
  5. srand in c or c++ doesn't really Generate Random Numbers ?
    By Application Development in forum c++
    Replies: 11
    Last Post: 07-10-2007, 03:21 AM