Trouble with rand and srand

Hi there,

Pretty new to C++ and OpenGL and this forum but help would be greatly appreciated. At the moment I'm trying to creating a simple animator program that changes with each run. The issue I'm having is that the animation is the same every time the program is run, clearly there is an issue with seeding. The code is as follow (for brevity I'm leaving out the main animation code)

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <glut.h>
#include <time.h>
#include <iostream>

using namespace std;

GLfloat radialX;
GLfloat DeltaRX = 0;
double movement = 0;

double LittleRadius = 0.2;
double BigRadius = 0.8;
double pi = 3.1415926;

double dmovement = rand();
double radialXmax = rand() % 100000 + 360;
double dradialX = rand() % 20 + 5;
double dDeltaRX = rand();
double colour1 = rand();
double colour2 = rand();
double colour3 = rand();

void Draw() {}

void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0);
}

int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1080, 1080);
glutInitWindowPosition(200, 200);
glutCreateWindow("lines");
Initialize();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}

Thanks in advance everybody.
Call srand once, at the beginning of main, before you call rand for the first time. For the sequence of numbers returned by rand to be different you need use a different seed each time. Often the return value of the time function is used.

 
srand(time(0));

Note that the time function (usually) returns the current time in seconds so if the program is started twice within the same second you will get the same random numbers. Often this is not a big issue because most programs tend to be started less often.
I see a mouch bigger problem here: Tons of global variables.
You should learn the basics of C++, then do some graphics programming (I would recommend so).
You don't even call srand... Or did I miss it?
Topic archived. No new replies allowed.