Controlling random() to generate integers when called upon
in
Programming Questions
•
11 months ago
Hi all,
How do I generate a random set of integers for an array (which determines endpoints for various lines) ONCE, and then when keyPressed, I can regenerate a new set of random integers?
I've created a function called LinearDeformation, and through a series of calculations I determine the endpoints for "major segments" which have a certain X range and Y range according to the width and height of the display. The random function determines a point within the determined ranges.
At the moment, the random function is continuously looping. I don't want to use noLoop(); because I want to be able to adjust the "margChange" continuously by pressing the arrow keys (and have real-time feedback). I've tried creating a separate function for integer generation but I'm having trouble figuring out how to structure it.
Here is the code, sorry if its illegible:
- float YmarginX= 20;
- float XmarginX = 20;
- float YmarginY= 20;
- float XmarginY = 20;
- float margChange = 1;
- float[] start;
- float[] range;
- float[] end;
- void setup(){
- size (500,500);
- smooth();
- //noLoop();
- randomSeed(0);
- //regenValues(Xmargin, Ymargin);
- }
- void draw(){
- background(255);
- // X directionality
- strokeWeight(3);
- LinearDeformation(XmarginX, YmarginX, margChange);
- // Y directionality
- strokeWeight(5);
- pushMatrix();
- translate(0,500);
- rotate(radians(270));
- LinearDeformation(XmarginY, YmarginY, margChange);
- popMatrix();
- }
- // */
- void keyPressed(){
- if (keyPressed){
- if (keyCode == LEFT ){
- if (margChange>0.1){
- margChange=margChange-(0.1);
- }
- }
- if (keyCode == RIGHT ){
- if (margChange<4.9){
- margChange=margChange+(0.1);
- }
- }
- if (key==' '){
- redraw();
- }
- }
- }
- void LinearDeformation (float Xmargin, float Ymargin, float margChange) {
- int numReps=5;
- float[] start = new float[numReps];
- float[] range = new float[numReps];
- float[] end = new float[numReps];
- float NEWheight = height-(2*Ymargin);
- //arrays for enpoint placement
- for (int s = 0; s < numReps; s++){
- if (s < 3){
- start[s] = random(Xmargin, ( ((s*.1)+.1) *width ));
- }
- else {
- start[s] = random(Xmargin, ( ((3-(s-2))*.1) *width ));
- }
- }
- for (int r = 0; r < numReps; r++){
- range[r] = random(Ymargin + (NEWheight*(r*.202)),
- NEWheight*(0.152+(r*.202)));
- }
- for (int e = 0; e < numReps; e++){
- if (e <=2){
- end[e] = random( ((.9- (e*0.1) ) *width), width-Xmargin);
- } else {
- end[e] = random( ((.9- ( (4-e) *0.1) ) *width), width-Xmargin);
- }}
- //accounting for margin change
- float newRange0 = (range[0])*margChange;
- float newRange1 = (range[1])*margChange;
- float newRange2 = (range[2])*margChange;
- float newRange3 = (range[3])*margChange;
- float newRange4 = (range[4])*margChange;
- //major segments
- line (start[0], newRange0, end[0], newRange0);
- line (start[1], newRange1, end[1], newRange1);
- line (start[2], newRange2, end[2], newRange2);
- line (start[3], newRange3, end[3], newRange3);
- line (start[4], newRange4, end[4], newRange4);
- //minor segments
- line (end[0], newRange0, end[1], newRange1);
- line (start[1], newRange1, start[2], newRange2);
- line (end[2], newRange2, end[3], newRange3);
- line (start[3], newRange3, start[4], newRange4);
- }
1