c++randomnegative-number

Random and negative numbers


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?


Solution

  • 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.