Random and negative numbers

前端 未结 7 766
鱼传尺愫
鱼传尺愫 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:15

    generate a random number between 0 and 2100 then subtract 100.

    A quick google search turned up a decent looking article on using Rand(). It includes code examples for working with a specific range at the end of the article.

提交回复
热议问题