diff --git a/content/00_randomness.html b/content/00_randomness.html index b473733c..5b831bfe 100644 --- a/content/00_randomness.html +++ b/content/00_randomness.html @@ -129,7 +129,7 @@
Taking this further, I could get rid of floor()
and use the random()
function’s original floating-point numbers to create a continuous range of possible step lengths from –1 to 1, as shown next.
step() { +step() { //{!2} Any floating-point number from –1 to 1 let xstep = random(–1, 1); let ystep = random(–1, 1); this.x += xstep; this.y += ystep; }-
All of these variations on the traditional random walk have one thing in common: at any moment in time, the probability that the walker will take a step in a given direction is equal to the probability that the walker will take a step in any other direction. In other words, if there are four possible steps, there is a 1 in 4 (or 25 percent) chance the walker will take any given step. With nine possible steps, it’s a 1 in 9 chance (about 11.1 percent).
Conveniently, this is how the random()
function works. p5.js’s random-number generator (which operates behind the scenes) produces a uniform distribution of numbers. You can test this distribution by counting each time a random number is picked and graphing those values.
What is the probability of drawing two aces in a row from a deck of 52 cards, if you reshuffle your first draw back into the deck before making your second draw? What would that probability be if you didn’t reshuffle after your first draw?
You can use the random()
function in a couple of ways to apply the concepts of probability in your code for a nonuniform distribution. One technique is to fill an array with numbers—some of which are repeated—and then choose random elements from that array and generate events based on those choices:
You can use the random()
function in a couple of ways to apply the concepts of probability in your code for a nonuniform distribution. One technique is to fill an array with numbers—some of which are repeated—and then choose random elements from that array and generate events based on those choices:
// 1 and 3 are stored in the array twice, making them more likely to be picked than 2. let stuff = [1, 1, 2, 3, 3]; //{!1} Pick a random element from an array. @@ -247,8 +247,7 @@Exercise 0.2
let num = random(1); +let num = random(1); // If the random number is less than 0.6 if (num < 0.6) { print("Sing!"); @@ -259,7 +258,6 @@-Exercise 0.2
} else { print("Sleep!"); }
Now let’s apply this methodology to the random walker so it tends to move in a particular direction. Here’s an example of a Walker
object with the following probabilities:
Another common use of this technique is to control the probability of an event that you want to occur sporadically in your code. For example, let’s say you create a sketch that starts a new random walker at regular time intervals (every 100 frames). With random()
, you could instead assign a 1 percent chance of a new walker starting. The end result is the same (a new walker every 1 out of 100 frames on average), but the latter incorporates chance and feels more dynamic and unpredictable.
Another common use of this technique is to control the probability of an event that you want to occur sporadically in your code. For example, let’s say you create a sketch that starts a new random walker at regular time intervals (every 100 frames). With random()
, you could instead assign a 1 percent chance of a new walker starting. The end result is the same (a new walker every 1 out of 100 frames on average), but the latter incorporates chance and feels more dynamic and unpredictable.
Create a random walker with dynamic probabilities. For example, can you give it a 50 percent chance of moving in the direction of the mouse? Remember, you can use mouseX
and mouseY
to get the current mouse position in p5.js!
Consider a class of 10 students who receive the following scores (out of 100) on a test: 85, 82, 88, 86, 85, 93, 98, 40, 73, and 83.
-The mean is the average: 81.3.
-The standard deviation is calculated as the square root of the average of the squares of deviations around the mean. In other words, take the difference between the mean and each person’s grade, and square it, giving you that person’s squared deviation. Next, calculate the average of all these values to get the average variance. Then, take the square root of the average variance, and you have the standard deviation.
-Score | -Difference from Mean | -Variance | -
---|---|---|
85 | -85 - 81.3 = 3.7 | -(3.7)^2 = 13.69 | -
40 | -40 - 81.3 = -41.3 | -(-41.3)^2 = 1,705.69 | -
. . . | -- | - |
- | Average Variance: | -228.21 | -
The standard deviation is the square root of the variance: 15.13.
+Consider a class of 10 students who receive the following scores (out of 100) on a test: 85, 82, 88, 86, 85, 93, 98, 40, 73, and 83.
+The mean is the average: 81.3.
+The standard deviation is calculated as the square root of the average of the squares of deviations around the mean. In other words, take the difference between the mean and each person’s grade, and square it, giving you that person’s squared deviation. Next, calculate the average of all these values to get the average variance. Then, take the square root of the average variance, and you have the standard deviation.
+Score | +Difference from Mean | +Variance | +
---|---|---|
85 | +85 - 81.3 = 3.7 | +(3.7)^2 = 13.69 | +
40 | +40 - 81.3 = -41.3 | +(-41.3)^2 = 1,705.69 | +
. . . | ++ | + |
+ | Average Variance: | +228.21 | +
The standard deviation is the square root of the variance: 15.13.
+What next? What if, for example, the goal is to assign the x-position of a shape drawn?
By default, the randomGaussian()
function returns a normal distribution of random positive and negative numbers with a mean of 0 and a standard deviation of 1. This is also known as the standard normal distribution. Often, however, these default parameters won’t work. For example, say you want to randomly assign the x-position of a shape by using a normal distribution with a mean of 320 (the center horizontal pixel in a window of width 640) and a standard deviation of 60 pixels. In this case, you can adjust the parameters by passing the randomGaussian()
function two arguments: the mean followed by the standard deviation.