Generating Vector of Random Number (of Size M) from -K to K in C++
I开发者_运维问答s there a fast way to do that?
I would recommend using the Mersenne Twister to generate uniform random variables. If you generate a uniform deviate u ~ [0,1] then (2 * K) * u - K will be ~ [-K, K].
I suppose you could do this with rand:
for (int i = 0; i < M; i++)
{
v.push_back(rand()%(2*K)-K;
}
But I need to know more about your question. Is the interval [-K,K] or (-K,K)? Do you include -K and K or not?
精彩评论