# 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 &lt;&lt; srand(2,12); cout &lt;&lt; srand(12); cout &lt;&lt; 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 ...

1. ## 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. ## 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.

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. ## 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
>
> 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.
>
> 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. ## 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. ## Re: How to use srand and rand?

<PencoOdStip> wrote in message
> 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.

6. ## 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
>> 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.
>

--
Clark S. Cox III
clarkcox3

7. ## 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
>>> 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 <<