Help me understand random() please
in
Programming Questions
•
1 year ago
This is what I started with;
- void setup() {
- size(600,250);
- smooth();
- }
- void draw() {
- background(0);
- drawRect(200, 125, 50);
- drawRect(220, 125, 100);
- drawRect(240, 125, 150);
- }
- void drawRect(float x, float y, float g) {
- fill(0, g, 0);
- for (int i = 0; i < 100; i++) {
- float h = random(10, 200);
- rect(x, y, 15, h);
- }
- }
So I'm trying to create a bar graph that is randomly generated within processing. I figured this would be a more easier task than trying out external data sources.
I got the random() function from the references page and edited it according to what I thought would be right. Though currently as it sits, it does kind of randomly generate a random height of sorts... Except it's not how I wanted the "random height" just seems to be three bars the exact same height with random black lines flashing inside of them... So I figured that I did use the random() function properly more or less.
I would like some explanation though,
This line here from my un knowing grasp seems to be generating between 0 and 100? I don't understand what this does at all, so I decided to wing it and get rid of the top line and just stick with:
I got the random() function from the references page and edited it according to what I thought would be right. Though currently as it sits, it does kind of randomly generate a random height of sorts... Except it's not how I wanted the "random height" just seems to be three bars the exact same height with random black lines flashing inside of them... So I figured that I did use the random() function properly more or less.
I would like some explanation though,
- for (int i = 0; i < 100; i++) {
This line here from my un knowing grasp seems to be generating between 0 and 100? I don't understand what this does at all, so I decided to wing it and get rid of the top line and just stick with:
- float h = random(10, 100);
it seems to actually generate a height, as soon as I drop noLoop(); into setup. But it seems to be generating this height in a backwards way with the bar graph being upside down, I figured that if I set random(10, 200); to random(-10, -200) that it would fix that there problem but that does not seem to be the case.
So as of now this is what I have...
So as of now this is what I have...
- void setup() {
size(600,250);
smooth();
noLoop();
}
void draw() {
background(0);
drawRect(200, 125, 50);
drawRect(220, 125, 100);
drawRect(240, 125, 150);
}
void drawRect(float x, float y, float g) {
fill(0, g, 0);
float h = random(-10, -200);
rect(x, y, 15, h);
}
:D Any help would be cool, I'll still be screwing around seeing if I can fix this.
1