Random and negative numbers

Max Frai picture Max Frai · Sep 30, 2010 · Viewed 49k times · Source

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?

Answer

Kendrick picture Kendrick · Sep 30, 2010

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.