Random and negative numbers

前端 未结 7 792
鱼传尺愫
鱼传尺愫 2021-01-17 14:52

I have to generate numbers in range [-100; +2000] in c++. How can I do this with rand if there is only positive numbers available? Are there any fast ways?<

7条回答
  •  一个人的身影
    2021-01-17 15:35

    You can use the C++ TR1 random functions to generate numbers in the desired distribution.

    std::random_device rseed;
    std::mt19937 rng(rseed());
    std::uniform_int_distribution dist(-100,2100);
    
    std::cout << dist(rng) << '\n';
    

提交回复
热议问题