# A newbie needs help

**URL:** <https://discourse.processing.org/t/a-newbie-needs-help/28832>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [March 25, 2021, 11:46am UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832 "2021-03-25T11:46:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiquip](https://avatars.discourse-cdn.com/v4/letter/x/df705f/32.png) [@xiquip](https://discourse.processing.org/u/xiquip)\
**Post date:** [March 25, 2021, 11:46am UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/1 "2021-03-25T11:46:53Z")

</div>

Hello Im dont have a clue about programming and at school they gave us some exercises  
Anyone knows how to set a random value to an alpha? For example, a circle, triangle or square.

for example , I assign a variable to some parameters like  
var triangleOpacity = 0;  
var triangle X =  
var triancle Y =  
var triangle size =  
fill(‘yellow’);

triangle(triangleX-triangleSize/3,triangleY, triangleX+triangleSize/3,triangleY, triangleX,triangleY-triangleSize/2 );  
}

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 25, 2021, 12:04pm UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/2 "2021-03-25T12:04:57Z")

</div>

Hello, @xiquip, and welcome to the Processing Forum!

Be careful with spelling in your program. Notice that you have this:

```auto
var triancle Y =

```

Please fix the spelling, and be aware that the name of a variable cannot include a space.

A useful resource for p5.js programmers is the [p5.js Reference](https://p5js.org/reference/).

That reference contains this: [Reference: `random()`](https://p5js.org/reference/#/p5/random). An alpha value can range from `0` to `255`, inclusive. It should be a whole number.

Let’s first see if you can use the information in the reference to assign an appropriate random number to `triangleOpacity` in your program. That will serve as the alpha value. Please post the statement that accomplishes that task here, and we will help you with refining it, as necessary.

For your program to execute, you will need to correct all the spelling mistakes, as well as assign values to all the variables. For example, you can do this:

```auto
  var triangleX = 120;

```

_Edited on March 25, 2021 to add the following:_

Your posted code is difficult to read, since it is not formatted properly for posting. See [Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147) for lots of useful advice, including information on how to format code for posting.

---

<div class="post-metadata">

**Author:** ![xiquip](https://avatars.discourse-cdn.com/v4/letter/x/df705f/32.png) [@xiquip](https://discourse.processing.org/u/xiquip)\
**Post date:** [March 25, 2021, 1:43pm UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/3 "2021-03-25T13:43:43Z")

</div>

Thank you @javagar very much!

var triangleX = 150;  
var triangleY = 300;  
var triangleSize = 300;  
var triangleOpacity = 10;

function setup() {  
createCanvas(400, 400);  
noLoop();  
}

function draw() {  
background(220);  
noStroke();

fill(255, 211, 25, triangleOpacity);  
triangleOpacity = random (0, 255) // Is this ok? Im not sure  
triangle(triangleX-triangleSize/3, triangleY, triangleX+triangleSize/3 ,triangleY, triangleX,triangleY-triangleSize/2 );

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 25, 2021, 2:00pm UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/4 "2021-03-25T14:00:42Z")

</div>

Thanks for posting your revised code.

Does it run and draw a triangle now?

This statement should be moved so that it executes before you call the `fill()` function:

```auto
triangleOpacity = random (0, 255) // Is this ok? Im not sure

```

Then the new value for `triangleOpacity` will always get used for the color.

With the `random()` function, the upper bound is exclusive. So in this case, you may want to use `random(256)` so that a result of `255` is possible. This would be even better:

```auto
  var triangleOpacity = floor(random(256));

```

Does your entire triangle show up on the screen? If part of it gets cut off as you are developing the program, you may want to adjust the arguments passed to the the `createCanvas()` function to make the canvas larger.

You may benefit by drawing a line on the screen before you draw the triangle. Then, whenever the triangle has some transparency, the line will show through it. You can use the p5.js Reference page to get information about drawing a line.

---

<div class="post-metadata">

**Author:** ![xiquip](https://avatars.discourse-cdn.com/v4/letter/x/df705f/32.png) [@xiquip](https://discourse.processing.org/u/xiquip)\
**Post date:** [March 25, 2021, 2:23pm UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/5 "2021-03-25T14:23:52Z")

</div>

Thank you very much @javagar Now it works!  
Thanks again for the quick answer

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 25, 2021, 5:30pm UTC](https://discourse.processing.org/t/a-newbie-needs-help/28832/6 "2021-03-25T17:30:01Z")

</div>

Best of luck with all your work, @xiquip! Build your skills by continuing to make good use of the p5.js Reference, and have fun being creative. 😄
