Basic question about ofRandom for color

I have the most basic of questions.

I am generating a series of circles using a for (int i=0; I < AMOUNT; i++); where AMOUNT is defined in ofApp.h

I want the colors of the circles to all be randomly defined when they are drawn and to retain the colors that they are assigned. I tried tried a number of options for defining the random color in setup(), in update(), in draw(), as well as in ofApp.h and I end up with one of two things: 1) either all objects end up being the same color; or, 2) they all change colors with every frame.

This is clearly something that can be accomplished. I just can’t seem to figure out how to do it.

Any insight is greatly appreciated. Thanks.

you can make an array of ofColors, assign random values on setup and in your draw you can call
ofSetColor(allColors[a]);

Later you can create a custom object that holds all atributes you want in one place, something like:

struct colorCircle {
public:
	ofColor color;
	int x, y, radius;
};

vector <colorCircle> circles;

What @dimitre points out is the ideal as it will only calculate the color once, yet there is a hackish way to avoid such and calculate the random color on each frame, yet always getting the same random sequence.

in draw()

ofSeedRandom(1234);// this is the line that does the trick, as it will reset the random number generator on each frame. you can change the number passed to it and you'll get a different random sequence.
for(int i =0; i < AMOUNT; i++)
{
ofSetColor(ofRandom(255), ofRandom(255), ofRandom(255));
// I like to use the following variation as it uses Hue, Saturation, Brightness values instead of RGB, allowing me to have random colors that are always bright, for example.
//ofColor color;
//color.setHsb(ofRandom(255), ofRandom(150 , 255), ofRandom(150, 255));
//ofSetColor(color);
ofDrawCircle( x, y, radius);// up to you to define x, y and radius
}

Thanks guys.

Neither of these is really working the way I was hoping. So, I thought I should share the full script.

@roymacdonald, I adding in the lines that you were recommending as you’ll see below. Two things are happening…

First, if I run the script what it produces is 100 circles, all the same color—but they don’t blink. What I was hoping to produce is 100 circles all different colors and stable—not blinking.

Second, the result it always that same color. A sort of teal. And, for some reason it appears as if the red channel is ignored completely. If I change the values in green and blue I get a slightly different result. If I zero our the green and blue and keep red at 255 then the circles are black.

Here is the code I have:

In ofApp.h…

#pragma once

#include “ofMain.h”

#define AMOUNT 100

class ofApp : public ofBaseApp{

public :

void setup();

void update();

void draw();

void keyPressed( int key);

void keyReleased( int key);

void mouseMoved( int x, int y );

void mouseDragged( int x, int y, int button);

void mousePressed( int x, int y, int button);

void mouseReleased( int x, int y, int button);

void mouseEntered( int x, int y);

void mouseExited( int x, int y);

void windowResized( int w, int h);

void dragEvent(ofDragInfo dragInfo);

void gotMessage(ofMessage msg);

vector< float > speedx;

vector< float > speedy;

};


In ofApp.cpp…
#include “ofApp.h”

//--------------------------------------------------------------

void ofApp::setup(){

ofSetFullscreen( true );

ofBackground(0, 0, 0);

ofSetCircleResolution(36);

ofEnableSmoothing();

speedx.resize(AMOUNT);

speedy.resize(AMOUNT);

for ( int i = 0; i < AMOUNT; i++) {

speedx[i] = ofRandom(.001, 0.09);

speedy[i] = ofRandom(.001, 0.09);

}

}

//--------------------------------------------------------------

void ofApp::update(){

}

//--------------------------------------------------------------

void ofApp::draw(){

ofSeedRandom(1234);

ofSetColor(ofRandom(255), ofRandom(255), ofRandom(255));

float time = ofGetElapsedTimef();

for ( int i = 0; i < AMOUNT; i++) {

float x = ofGetWidth() * ofNoise(time * speedx[i]);

float y = ofGetHeight() * ofNoise(time * speedy[i]);

ofDrawCircle(x, y, 20);

}

}

//--------------------------------------------------------------

void ofApp::keyPressed( int key){

}

//--------------------------------------------------------------

void ofApp::keyReleased( int key){

}

//--------------------------------------------------------------

void ofApp::mouseMoved( int x, int y ){

}

//--------------------------------------------------------------

void ofApp::mouseDragged( int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mousePressed( int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mouseReleased( int x, int y, int button){

}

//--------------------------------------------------------------

void ofApp::mouseEntered( int x, int y){

}

//--------------------------------------------------------------

void ofApp::mouseExited( int x, int y){

}

//--------------------------------------------------------------

void ofApp::windowResized( int w, int h){

}

//--------------------------------------------------------------

void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------

void ofApp::dragEvent(ofDragInfo dragInfo){

}

Hi,
you need to put the ofSetColor(…) inside the for loop, as in the code snippet I posted.

Then, when posting code, select it in the text box and clic the </> button and it will be rendererd as code, making it much easier to read.

Best.