Seeding a random number

I am trying to create a random number generator with a random seed. The problem I am having is that I am trying to define my randomized variable as a global variable so I have put the rand statement in the first line.

This seems to always error out.

So I tried to put the srand(time(NULL)) statement under main() under the main.cpp file and then my ofApp.cpp can not find the srand statement and again rand does not have a random seed.

From what I can tell I can only globally define variables because when I try to define anything else code::blocks complains that I do not have a main() statement preceding it.

I think this may be confusing so here is the code I am trying to use

#include “ofApp.h”

srand (time(NULL));
int start = rand()%500+30;

int xCord = start;

Does what I am asking make any sense?

Maybe it’s easier if you use ofSeedRandom. As an example:

ofApp.h

int xCord = 0;

setup()

ofSeedRandom(); // or ofSeedRandom(int val);
xCord = ofRandom(500) + 30;

you should avoid globals. They are never really necessary (espacially in oF!)

But here’s what’s going on:
each .cpp file is compiled independently, so one is not aware of the contents of the other. To resolve this problem we use header files, which contain information about the contents of a .cpp.

To achieve what you want in oF you should do as @hubris recommended. Or, if you really want to use your function, just replace his setup code for yours.

Okay so I gave it a try as hubis suggested and it worked! But I am still a little confused. My teacher told me in class that all variables must defined in the “ofApp.h” section because If we define a variable in say “ofApp::update()” then no other section will be able to see the variable.

What I am seeing now is that you should define all variables up there however functions do work throughout the different sections?

So in other words if I am understanding this correctly I do want global variables and I do not want global functions?

what your teacher said is true, but remember that there’s a difference between declaring a varible and assigning a value to it. Everything in C++ has a scope When you do

void ofApp::setup() {
int x;
}

the variable X is destroyed as soon as the function call is over. However, if you declare a variable in the class body, that variable belongs to the class as a hole, and will have the same life span as the variable itself.

class ofApp : public ofBaseApp {
(...)
int x;
}

In this code, the variable X can be altered and seen by all the functions that belong to the class ofApp. That way you can later assign a value to it whenever you want

void ofApp::setup(){
x = 0;
}
void ofApp::update(){
x++;
}

the class ofApp is created in main.cpp in the line

ofRunApp(new ofApp());

and is only destroyed when the app window is closed.

In general, when working with the basics of openFrameworks, your whole program will be contained in the ofApp class. So, if you want a variable to be “global” you only need it to exist in the scope of the class ofApp. In other words, you need to declare it in the ofApp.h file, inside

class of App: public ofBaseApp{

}

To better understand scopes and duration of variables, take a look at this article.
This site helped me a lot when I was begining, hope it helps you too!

1 Like