Learning Objectives

Students will be able to:

Preparation

Chapter Questions

Put your two chapter questions here. Please include your name.

Sydney Goldberger:

  1. At the end of the modeling a deck with an array example, you write "return v" — what does this do / mean for the code? what exactly are you asking it to 'return'?

    The valueFromDeck function chooses a value from the array that represents the deck. It "returns" the value, meaning that when the function is "called" it will resolve to that value. console.log(valueFromDeck()) calls valueFromDeck, the function does its work and returns the value, and then console.log receives the returned value.

  2. In the small multiples study example challenge, were you asking us to draw every circle with the same random color or draw each circle with a different random color?

    I meant draw each circe a different randomly-chosen color, but doing both is a good idea.

Nikki Makagiansar:

  1. Do you mind elaborating on the logic behind why we shouldn’t call random multiple times when making one decision?  So it’s best practice to store random in a variable first and then use that variable throughout the respective conditional statements?

    lets say you wanted to do THING A or THING B with a 50/50 chance.
    
    something like the following would do that:
    
    r = random()
    if (r < .5) do THING A
    if (r >= .5) do THING B
    
    In this case THING A and THING B are mutually exlusive. They each
    happen 50% of the time, never at the same time, always one or the other happens.
    
    something like the following would kind of do that but not quite right:
    
    if (random() < .5) do THING A
    if (random() >= .5) do THING B
    
    In the latter case we call random() twice so the chance of THING A 
    is indepent of the chance of THING B and while independently both 
    happen 50% of the time there is a chance of them both happening at once
    or neither happening at all.
    
    you might try to fix this with `else`
    
    if (random() < .5) {
    	do THING A
    } else if (random() < .5) {
    	do THING B
    }
    
    This is closer, but still not quite there. THING A has a 50% chance of happening. 
    THING B won't happen if THING A does. Good so far, but...
    THING B only has a 50% chance of happening when THING A does not.
    THING B should happen *every time* THING A does not.
    There is now a 25% chance of nothing at all happening. 
    
    
  2. What would the histogram distribution for randomSeed() look like? Would it resemble even distribution? Is there a use or sketch case where it may be good to use this?

    random() should have an even distribution (or at least very close). randomSeed() doesn't really have a distribution because it doesn't return anything, it just selects which sequence random() produces

Gretchen Steinbrunner —

One = there's a one minute delay when I input changes to the Coding Challenge. Do you know why that is? I tried it in both Safari and Chrome.

no. that seems really strange... can you show me?

Two = If Math.round() leads to unevenly distributed results, then in what circumstance is it best applied? Also, are Math.round, Math.floor, and Math.random used in conjunction with randomSeed() in P5?

Math.round() and round() are a fine functions, and great when you want to round numbers to the nearest integer. Its just not the go-to for turning random floats into integers.

Math.random() and p5's random() function do not share the same underlying 'engine'. so randomSeed() does impact random(), but has no impact on Math.random()

Three = what is clear()?

huh, i just looked up clear() and the description isn't very clear to me at all. I'm pretty sure that clear will set every pixel on the canvas to RGBA 0,0,0,0. you should then be able to see through the canvas to the webpage below. you couldn't do that with background() because background(0,0,0,0) would draw with 0 alpha which would have no effect

Also, I thought of Terry Riley's In C when it comes to randomness and attempting to control randomness — https://en.wikipedia.org/wiki/In_C

this is great. thanks

spotify link

Becky Greubel:

  1. I'm still getting used to p5, and noticed moments in the code where width and height were called but weren't defined as a variable. In these cases, do they refer to the width and the height of the canvas?

    they do. width and height are global variables that are set by p5 automatically

  2. Would there be any way to have an edge bias in randomization? IE: bias towards the extremes (and inverted normal distribution?)

    yes. this would be a useful addition to the chapter. i'm trying to think of an *elegant* way to get there. here is my first thought

    let r = (random() + random()) * .5;
    // r is a middle biased random value in range 0 to 1
    r = (r + .5) % 1;
    // shift r so the middle of the "hill" is at 1, then use modulus to wrap
    // the right side of the hill down to the 0-.5 range.
    
    function setup() {
    	createCanvas(windowWidth, windowHeight);
    	background(100);
    }
    
    function draw() {
    	noStroke();
    	fill(255, 50);
    	for(let x=0; x<100; x++) {
    		ellipse(random(width), edgeRandom() * height, 20, 20);
    	}
    }
    
    function edgeRandom() {
    	let r = (random() + random()) * .5
    	return (r + .5) % 1;
    }
    

Maddie Hyunseung lee:

  1. I’m not sure why floor(random(1, 6)); won’t work?

    It would provide a random int in the set 1,2,3,4,5. It would not contain 6.

    random(1,6) returns a value between 1(inclusive) and 6(exclusive).

    since it doesn't return 6 or any number between 6 and 7, there are no numbers that would floor down to 6

  2. Am I understanding the meaning of graph correctly?: The height of each graph represents the possibility of the cases that can occur? If not, is it the value of min(random(10, random(10));, for example?

    the graphs are the expected likelyhood of each value

Elliot Bohlen

  1. I would like more info on LCGs, I don't think I understand the concept, is using seeds a way to refine the random values? If so, how is that different from doing something like random() *6 + 10 to veer towards a particular range of values

    we'll talk about LCG's more in class

    no, LCGs are not a way to refine/alter/bias/range the random values returned by random()

    LCGs are one type of algorithm for generating pseudorandom number sequences. they are what powers the random() function.

    or at least they could be, there are multiple PRNG alogrithms, I'm not sure what p5 uses

    okay, just looked it up (p5.js is open source after all!)

    https://github.com/processing/p5.js/blob/main/src/math/random.js

    looks like p5 does indeed use a LCG PRNG

    again, we'll talk through this in class

  2. Are random values technically pseudorandom? Are LCGs the way to create a truly random number?

    random values produced by Math.random() and by random() are technically pseudorandom.

    LCGs produce pseudorandom numbers

    computers (that aren't broken) can't generate actual random values, because they are deterministic

    creating true random numbers requires measuring something non-deterministic

    computers *can* do that

Amber Hurwitz

  1. For random() arguments, when does the "up to, but not including" rule apply? Is that only in the case of the default arguments for random(), i.e. (from zero, up to but not including one) as well as when a single number is passed as an argument for random()? (I.e. random(33) - sets the range from 0 up to but not including 33), and when two arguments are given? Is "up to but not including" the max argument always the case w/ random()?

in all those cases the "max" is not included

[ means inclusive, ) means exclusive

random() is [0-1)

random(3, 5) is [3-5)

random(3) is [0-3)

  1. Why can't it include the max argument number?

You * could * make random() be inclusive on both ends (probably, there are some weird little details to consider about floating point precision), but it would be less desirable in practice probably every time

because you are pulling float/decimal numbers from a continuous range

imagine you wanted to choose a random second from 2020

randomSecond(2020, 2021)

you'd want that to be inclusive of 2020: i want the first second of 2020 to be possible

you'd want all the other seconds in 2020 also

you'd want that exlcusive of 2021: i don't want the first second of 2021

it would be unusual that you'd want "any second in 2020 and maybe the first second of 2021"

i've never tried that explination before, i hope its clear

  1. Does random() * 5 + 10 say the same thing as random (10, 15)? yes, the result should be the sameWhy use the former syntax vs. the latter? i wouldn't. but Math.random() doesn't take parameters, so i'd have to if i was using that library instead of p5sAnd does the former also exclude the max argument (15)? they both would

    [0,1) * 5 -> [0,5)

    [0,5) + 10 -> [10,15)

Shivanjali Verma (Shivi) :

Que1. In the code for 'Deck Visualizer' - you've used a function called 'Shuffle', which is a p5.js function and used to shuffle the deck like how we normally do. But given the numbers in the array, every time we shuffle it will give different number so why did we have to increment by "position++" : ?

// change the position for next time

position++;

// if we run out of "cards", shuffle and start over from the top

if (position == values.length) {
    values = shuffle(values);
    position = 0;
  }

if we shuffled every time, we'd be getting essentially the same result as random(), each time we'd get a completely independent random result

we want something less random than that. we want to make sure every value in the deck is picked before any value is picked twice

Akshay Baweja

Materials