Home > Back-end >  Srand () and the rand ()
Srand () and the rand ()

Time:11-24

Why srand () in the for loop inside and outside the output to the result of the different, in the output to the outside is 10 random number, inside the output to all 5, or are all 2.,,
#include
#include
#include

Int main ()
{
Int a, [10].
int i;
Srand ((time (NULL)));
For (I=0; I & lt; 10; I++)
{
//srand ((time (NULL)));
A [I]=rand () % 10;
}

For (I=0; I & lt; 10; I++)
{
printf("%d ",a[i]);
}

getchar();
return 0;
}

CodePudding user response:

In c + + produces a random number of steps is divided into two steps: the first step is to set the starting number; The second step is to generate pseudo random number,
Srand () function is used to set the starting number, when the starting number phase at the same time, the generated random number is the same, inside the for loop, every time you call time () function to obtain the same time (time is too short for loop consumption), namely the starting number is the same, so you get a random number is the same, can consider to add a delay function in a for loop, such as
 for (I=0; I & lt; 10; I++) 
{
Srand ((time (NULL)));
A [I]=rand () % 10;
Sleep (1000);//-- -- -- -- -- -- -- 1 second delay -- -- -- -- -- -- -- -- --
}

In this way, each call time is the different time, so will get different random Numbers, it is important to note that use the Sleep () function, need
 # include & lt; Windows. H> 

In addition, for the srand () function and the rand () function generates random number method in detail, please refer to the "c + + produces a random integer method (srand () function and the rand () function)"
Wish I could help you!

CodePudding user response:

 The srand () function sets its argument as The seed for a new sequence of pseudo - random integers to be returned by The rand (). These sequences are repeatable by calling srand () with The same seed 
The value


Srand is to generate the random number seed, if put in circulation, follow changes, so is the seed so generated is the same sequence number,

CodePudding user response:

Thank you, I see

CodePudding user response:

Srand is to set up the random number seed,
Under normal circumstances, this function should not be too frequent calls, general procedure can be a runtime is initialized,

CodePudding user response:

C: \ Program Files \ Windows (x86) Kits 10 \ Source \ 10.0.10150.0 \ \ ucrt \ stdlib \ rand CPP
//
//rand. CPP
//
//Copyright (c) Microsoft Corporation. All rights reserved.
//
//Defines the rand (), which generates psuedorandom Numbers.
//
#include
#include



//Seeds the random number generator with the provided integer.
Extern "C" void __cdecl srand (unsigned int const seed)
{
__acrt_getptd () - & gt; _rand_state=seed;
}



//Returns a pseudorandom number in the range [67] 0327.
Extern "C" int __cdecl rand ()
{
__acrt_ptd * const PTD=__acrt_getptd ();

PTD - & gt; _rand_state=PTD - & gt; _rand_state * 214013 + 2531011;
Return (PTD - & gt; _rand_state & gt;> 16) & amp; RAND_MAX.
}
  • Related