有一段代码:
void GenerateData(int nPoints, int nDims, std::vector<float> & data)
{
unsigned int seed = 111;
data.resize(nPoints*nDims);
for (int i = 0; i < nPoints; i++)
for (int j = 0; j < nDims; j++)
data[i*nDims + j] = ((float)rand_r(&seed)) / RAND_MAX;
}
现在想在windows的vs2013中使用,我考虑使用srand()+rand(),
请问srand()的位置应该在哪里?
First of all, rand is a stateful sequence, which can be understood as a sequence.
And srand is a function that changes the starting state of this sequence.
srand only needs to be set once, after the program starts.
rand()
,srand()
andRAND_MAX
are all random number library functions in C. If using C++11 and above, the subject may consider using std::uniform_int_distribution or std::uniform_real_distribution in STL to implement random uniform distribution. If you need other distributions, you can also refer to Pseudo-random number generation. This should make the implementation platform independent.