Rand() function does not seem to randomize past low limit.

Hi everyone, I am almost finished with my project but my rand() function doesn’t seem to be working. I have two global variables set as:


const GLint CUBE_HIGH = 15;
const GLint CUBE_LOW = 7;

and the number of cubes I am getting is only 7. I cannot seem to figure out why so I am hoping someone can point out my mistake and show me what I need to do to fix it. Here is what I believe is the relevant code:

The function call to create the amount of cubes and the cubes random x & y axis which is called in main:


void initOrigins()
{
	maxCubes = rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW) + CUBE_LOW;
    for (int i = 0; i < maxCubes; i++)
    {
        cubeOrigins[i].x = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);
        cubeOrigins[i].y = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);
    }
}

and this is my display function:


void myDisplay()
{
	//maxCubes = CUBE_LOW + rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    for(int i = 0; i < maxCubes; i++)
    {

        glLoadIdentity();

        glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);
		glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
        glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

        glutWireCube(2.0f);
    }

	cubeZ += 0.050f;
    glutSwapBuffers();
    glFlush();

	if (cubeZ > 120.0f)
	{
		cubeZ -= 120.f;
		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		for(int i = 0; i < maxCubes; i++)
		{
			glLoadIdentity();

			glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, -120.0);
			glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
			glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

			glutWireCube(2.0f);

		}
		cubeZ += 0.050f;
		glutSwapBuffers();
		glFlush();
	}
}

Thank you everyone.

Please note that you question has nothing to do with OpenGL and you are likely to get better answers in a general programming forum.
In rand() / MAX_RAND you are performing integer division (and therefore the division always yields 0 - unless rand() happens to return MAX_RAND), cast one of the operands to a floating point type to get a floating point value between zero and one.

[QUOTE=carsten neumann;1255455]Please note that you question has nothing to do with OpenGL and you are likely to get better answers in a general programming forum.
In rand() / MAX_RAND you are performing integer division (and therefore the division always yields 0 - unless rand() happens to return MAX_RAND), cast one of the operands to a floating point type to get a floating point value between zero and one.[/QUOTE]

Thank you for the help. I have corrected it.


void initOrigins()
{
	maxCubes = rand() % (CUBE_HIGH - CUBE_LOW + 1) + CUBE_LOW;
    for (int i = 0; i < maxCubes; i++)
    {
        cubeOrigins[i].x = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);
        cubeOrigins[i].y = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);
    }
}