2.5: The random() Function – p5.js Tutorial

In this video, I teach you how to generate random numbers with the random() function in p5.js.

Next video:

Support this channel on Patreon:


Send me your questions and coding challenges!:

Link to code on Github:


For More p5.js Videos:

Help us caption & translate this video!






32 responses to “2.5: The random() Function – p5.js Tutorial”

  1. DerEddie LoL Avatar

    when i try to add
    var spot = {
    x: 10;
    y: 10;
    to my code the whole programm stops working….

  2. DerEddie LoL Avatar

    i got some problems with the variables which i declare…. i declared in setup/draw/outside all functions and sometimes the programm stopped working because of it … advise? not sure where to put my variables

  3. Oki Virgiawan Avatar

    Hahaha good tutorial i always happy see your tutorial video, usually people have to serious to coding but not you

  4. nexusbug Avatar

    "Oh look, some crazy stuff has happened here, some horrible stuff. This video is a disaster"

    I have never seen a disaster in such an awesome way.

  5. Zyaaco Avatar

    can somebody help me ;(

    var xr = random(0, 600);
    var yr = random(0, 400);

    function setup() {

    function draw() {

    for(var i = 0, i < 30 , i++) {
    ellipse(xr, yr, 10, 10)

    dosent work when i put the random function

  6. Rami Har Avatar

    I could make it, hope you see this:

    var spot = {
    x: 100,
    y: 100
    var col = {
    r: 100,
    b: 100,
    g: 255,
    a: 200

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

    function draw() {
    spot.x = random(0, 400);
    spot.y = random(0, 400);
    col.g = map(spot.x, 0, 400, 100, 255);
    col.b = map(spot.x, 0, 400, 255, 100);
    fill(col.r, col.g, col.b, col.a);
    ellipse(spot.x, spot.y, 16, 16);

  7. Adolfo Carrillo Avatar

    Man I´m Watching all of your tuition videos and all of them are great, you must be a great teacher. In the begining of the series when you introduce yourself I heard that you last name was Shitman and I thought, poor man such a bad last name for this incredible man, but then I realice it was Shiffman.

  8. Tongki Ilagan Avatar

    what code editing software would you recommend that's free?
    i love the series man it inspired me to push through pursuing maybe computer science in the future

    the part where u changed point to spot 6:32 cracked me up btw xD

  9. srta Thunder Avatar

    love the way you explain it all 😀

  10. Alex Wang Avatar

    Is it possible to use random(0, 255) inside the Color object for r, g, b? If so, how can I do it? I tried doing this and it's telling me the colors are undefined and I'm hoping that I wouldn't need to give color some dummy numbers and then override it with random later in my code.

  11. M S Avatar

    If you would have been one of my teachers in highschool, I wouldn't have hated it that much. School is boring…

  12. stomnish Avatar

    too much coffe…

  13. James Aylward Avatar

    Reference error: height is not defined

    Same thing with width. Help?

  14. Rean Itt Avatar

    I love this! It's great!

  15. DEADWAVE Avatar

    so perfect, much programming, such wow

  16. Noah Payne Avatar

    I learn more in your videos than I do in school most of the time. I absolutely love your videos and hope that my JavaScript knowledge and my python and partial Java knowledge will hopefully lead me to my career interest of being a software developer. Just wondering though, why do you always worry about the time of your videos?

  17. anish adhikari Avatar

    I've been watching these tutorials serially but this one is cool. XD
    Maybe we should make our boring teachers to watch this video and aware them how teaching should be done 😀

  18. Anon Ymous Avatar

    I wanna see your replies to some comments, why did you delete them?

  19. lefteris soulis Avatar

    hello great vids i have a question, how can you generate random ellipses one after another?

  20. Darsh Shukla Avatar

    Man you are awesome !!
    I made a cool shape_changer canvas :

    function setup() {
    createCanvas(screen.width, screen.height);

    function draw() {
    var r = random(0, 255);
    fill(random(0,255), 0, random(0,255),50);
    stroke(random(255), 0, random(0,255));
    //different shapes
    s = floor(random(0,3))
    if (s===0) {
    ellipse(random(width), random(height), r, r);
    if (s===1) {

    ellipse(random(width), random(height), random(0, 255), random(0,255));

    if(s===2) {

    rect(random(width), random(height), random(0, 255), random(0, 255));


  21. Ivan Nedelkovski Avatar

    These videos are great, thank you for making them! p5.js is so fun =)

  22. Jason Thomas Avatar

    I agree about keeping the issue in the video. Problem-solving is what programming is all about. And, it shows how even the most experienced coder can have simple issues like this. I can't say enough good things about your videos. Great stuff.

  23. Jason Thomas Avatar

    Wooooo Saaaaaa, Woooooo, Saaaaaaaaa 12345678910
    P.S. Excellent videos by the way. I wish that I'd known about your channel when I got started. Not boring, clearly explained. Great refresher for people that haven't programmed in a while.

  24. Daniel Boel Nielsen Avatar

    isn't this a bit POINTless?

  25. Savage Noble Avatar

    "I've done something horrible! This is great!"

    I love this. More people should approach learning in this spirit.

  26. August Gislason Avatar

    "And everything exploded, and i got a weird error message" lol xD

  27. Dennis Kluger Avatar

    Hello Daniel
    I´ve been watching your videos for a couple of days now and i was wondering how do you highlight your text in the editor ? For example the numbers are kinda orange.
    Great videos you are helping me a lot.
    Keep going

  28. Mr. Guy Avatar

    Hey. I created a simple file in p5.js but it won't work. Can I have some help and if so, where do I reach you to send the file?

    P.S. I found your videos yesterday and I love them. You are amazing!

  29. Alex Steventon Avatar

    Why can't I set random when setting a var?

    Eg, var spotrad = random(10,50)

  30. Siddharth Maurya Avatar

    Wow!! Sir, I'm enjoying learning with your style! It's AWESOME! It's Amazing!!

Leave a Reply

Your email address will not be published. Required fields are marked *