The random number C on the random seed

//srand(time(0));
for (int i = 0; i <1000; ++i)
{
int b=rand();
printf("num is %d\n", b);
}

I found the use of random seed, can get a random number.. The seed, is used to doing.

Started by Rodney at February 10, 2016 - 3:51 PM

You run the application again will find the number 1000 output
And number 1000 the last output as like as two peas
That is the role of srand

Posted by Selina at February 11, 2016 - 4:18 PM

As the seed with the current time, because the running time is random, so as to get the result of random.

For the same seed, a random number generator to generate a random number sequence, the same can be seen, in fact, the random number generator is a pseudorandom number.

Posted by Matthew at February 24, 2016 - 4:36 PM

Random number with pseudo random number it means life trajectory is established is a random number will repeat the first random

And the time for the seed word is repeated for random
Because time is defined as the 1990 day of the month (I forgot) 0 to now after the number of seconds is forever go down in value and the]
Until it overflows so far but I feel we are not living at that time.

Posted by Alexandra at February 26, 2016 - 4:38 PM

srand()Is used to initialize the random seed number, because the RAND internal implementation is the linear congruential method to do, he isn't really random, but because it is a particularly long cycle, so there is a certain range can be regarded as random, formula is as follows:
rand = rand*const_1 + c_var;

The srand function is to give its first Rand value.

No srand function, the random seed was 1

Posted by Harry at March 09, 2016 - 4:55 PM

Pseudo random´╝ü. .

Posted by lilian at March 14, 2016 - 5:15 PM

Pierce to the heart of the matter

Posted by Leander at November 08, 2016 - 10:07 AM

Understand. .

One more question.
The use of two Rand would improve his random sex?
int b=(rand()+rand())/2;
printf("num is %d\n", b);

Posted by Rodney at November 12, 2016 - 10:32 AM

Not. . . .

Posted by Frieda at November 15, 2016 - 10:48 AM

Have a look the srand documentation

Posted by Stella at November 25, 2016 - 11:04 AM

Of course not.

Posted by Frederick at November 27, 2016 - 11:28 AM

Don't open of
The 1 is the value of B
Then 2 times the return value of Rand 3 may
0 1
1 0
1 1

The value of B is the maximum Rand
Then 2 times the return value of Rand is only 1 possible
Maximum value

Originally, the value of B is 1 probability should be and the value of B for the maximum probability is the ideal state of the same [down]
Now the maximum probability only other number 1/3
This situation is to reduce the randomness

Posted by Selina at December 10, 2016 - 12:25 PM

A b value is 1
Should the corresponding
1 1
1 2
2 1

Be ashamed

Posted by Selina at December 12, 2016 - 12:49 PM

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}

Posted by Emma at December 15, 2016 - 12:52 PM

Remember, the value of the random number is determined by the last seed. Returns the current value, as the next seed

Posted by Phoebe at December 30, 2016 - 1:44 PM

Do not be random. .

Posted by Algernon at January 08, 2017 - 1:48 PM

Random number is generated randomly

Posted by Chasel at January 09, 2017 - 2:18 PM

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\tidtable.c
386:    ptd->_holdrand = 1L;

Posted by Emma at January 09, 2017 - 2:39 PM

No, Rand () is a pseudo random number, if no srand (), each time you print out (rand) are fixed.

Posted by Page at January 12, 2017 - 3:37 PM