2

I have a window size {0,0},{1e6,1e6}. I have created grids of 1000*1000 within this area.
I am trying to create random points in this area.

glEnable( GL_POINT_SMOOTH );
glEnable( GL_BLEND );
glPointSize( 200 );
for( int i = 0; i < 1e6; i++ )
{
    glColor3ub(0,255,0);
    // width and height are 1e6.
    int x = rand() % WIDTH; 
    int y = rand() % HEIGHT; 
    glBegin( GL_POINTS );
    glVertex2i( x, y );
    glEnd();
}
glFlush();  
glDisable( GL_BLEND );

x and y are random numbers. However, all points are drawn only in left bottom of the window.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1302064
  • 81
  • 2
  • 10
  • Did you call glViewport? What were the arguments to it? You're viewport might not be the rectangle (0, 0)->(WIDTH, HEIGHT) – Jacob Parker Oct 10 '12 at 19:14
  • I have passed the actual width and height. Random numbers generated i guess are too small so they are appearing only in the bottom left corner. I want evenly distributed points. Could you please tell me if there is any better approach? – user1302064 Oct 10 '12 at 19:38
  • I want to generate 1000000 points – user1302064 Oct 10 '12 at 19:41
  • I gave some example code below. Is it what you wanted? – Jacob Parker Oct 10 '12 at 20:37
  • x = rand()/(float)RAND_MAX; y= rand()/(float)RAND_MAX; discs[i].x=x*WIDTH; discs[i].y=y*HEIGHT;Thank you so much. With this code it worked – user1302064 Oct 10 '12 at 21:39

1 Answers1

0

Ensure that RAND_MAX on your platform is greater than 1e6. Otherwise the largest x/y value you will see will be RAND_MAX-1, not 1e6 as you're expecting.

Or generate random numbers without modulo.

Community
  • 1
  • 1
genpfault
  • 51,148
  • 11
  • 85
  • 139