c++ - 如何将linux中的rand_r()在windows下实现?
巴扎黑
巴扎黑 2017-04-17 13:41:18
0
2
1308

有一段代码:

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()的位置应该在哪里?

巴扎黑
巴扎黑

reply all(2)
阿神

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() and RAND_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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!