Generate random number

srand (time(NULL));
for(int i=0; i<N; i++)

Im trying to generate random numbers between -32767 and +32767, some help as to where im going wrong would be helpful as this code generates only negative numbers
cout << RAND_MAX << endl; will give me
on my compiler. It means that rand() will not generate numbers larger than that. And you sustract 32767 from generated number, so...

Use C++11 random generators.
This: should suit you.
You might try:
int n = rand() + rand() - RAND_MAX;

which should give a random number in the range
What you say is true but the random numbers will not have a uniform distribution. n is more likely to get values close to 0.
Last edited on
Unbiased scaling of random numbers is somewhat tricky.

MiiNiPaa's suggestion: +1
Topic archived. No new replies allowed.