Single-use Random vs. ofRandom

#1

I’m not sure what the proper nomenclature is for this stuff, since I’m just learning - so please excuse my mislabeling for the time being.

My understanding is that ofRandom is a random function - and functions evaluate at frame rate within the draw() and update() functions.

I’m working with the “singleLightExample” in the examples folder - and part of the code uses a for loop:

for(int i = 0; i < numSpheres; i++) {
float angle = TWO_PI / (float)numSpheres * i;
float x = cos(angle) * radius;
float y = sin(angle) * radius;
ofDrawSphere(x, y, -200, sphereRadius);
}

I’d like to generate a random value for each sphereRadius, one time.

Is the best way to do this to somehow populate an array in the setup() function with one-time generated values and then find a way to feed them into the for loop?

#2

You’re on the right track.
setup() is run once, so setting up an array of random sphere radius in setup() is the way to go.
You can use ofRandom(min,max) to set the range of the radius.

#3

Hi JDeut, you will need a data structure to store the radius, maybe a vector, try to declare in ofApp.h something like:
std::vector<float> spheresRadius;
Then, in the setup method, set the values for each radio with a for loop, for example

	for (int i = 0; i < numSpheres; i++) 
	{
		spheresRadius.push_back(ofRandom(80,220));//Set the  sphereRadius values between 80 and 220
	}

And finally, in the draw method, change the line that draw the spheres with the values that we have stored in our vector sphereRadius:
ofDrawSphere(x, y, -200, spheresRadius.at(i));

1 Like
#4

Thanks Veketor - that definitely works - but just so I can fully learn the lesson:

Can you recommend any documentation/writing re: data structures in OF?
I’m not exactly sure what “std::vector” means, even though I think I know what a vector and float are.

#5

Hello @JDeut, take a look at https://openframeworks.cc/ofBook/chapters/stl_vector.html, is a great online book to learn c++ and openframeworks.

2 Likes