Mar 14, 2013 at 6:40am UTC
srand (time(NULL));
for(int i=0; i<N; i++)
{
points[i].x=(rand()%(32767-(-32767)))+(-32767);
points[i].y=(rand()%(32767-(-32767)))+(-32767);
cout<<"x="<<points[i].x<<endl;
cout<<"y="<<points[i].y<<endl;
}
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
Mar 14, 2013 at 11:08am UTC
You might try:
int n = rand() + rand() - RAND_MAX;
which should give a random number in the range
-RAND_MAX to +RAND_MAX
Mar 14, 2013 at 11:17am UTC
@Chervil
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 Mar 14, 2013 at 11:17am UTC
Mar 14, 2013 at 11:57am UTC
Unbiased scaling of random numbers is somewhat tricky.
MiiNiPaa's suggestion: +1