sure!
notice how every time you run that sketch the ‘barcode’ is always the same. it’s because it’s all drawing from the same seed ( in a sense, picking the numbers up one by one from the glue, it’s still generating 100 random numbers, but they are the random numbers that got shaken up and stuck down at the beginning of the sketch. and if we try to shake up the bucket again, we’ll get the same barcode.
for proof of this, compare to this alternative example which is in the draw loop and has no seed, essentially we’ve got no glue so each time we draw the random number the bucket gets shaken up and we get a random one each frame:
function setup() {
createCanvas(400, 400);
}
function draw() {
for (let i = 0; i < 100; i++) {
let r = random(0, 255);
// console.log(r)
stroke(r);
line(i, 0, i, 100);
}
}
compare to the reference example where we first set the seed (glue down the randomness) then grab numbers out of the bucket in the way they are oriented from the start. we can put the same code in the draw loop with a seed where it’s trying to ‘shake the bucket’ each frame, but since everything is already glued down it wont really matter. We’ve got an initial randomness, but not randomness on each frame.
function setup() {
createCanvas(400, 400);
}
function draw() {
randomSeed(99);
for (let i = 0; i < 100; i++) {
let r = random(0, 255);
// console.log(r)
stroke(r);
line(i, 0, i, 100);
}
}
another way to think about it:
you’ve got a wall and you’re going to throw a dart at it and try to hit a number written on a piece of paper. lucky for you, you’ve got perfect aim and you’ll always hit the same spot.
generating a random number is like having a friend thrown a handful of paper slips into the air which the dart hits and pins to the wall, this is a random number.
random seed would be like asking a friend to randomly pin them to the wall ahead of time, and then when you throw the dart, it hits one of the slips that you’re friend happened to pin up.
You move (generate another random number) each time you thrown the dart, and it hits another slip, but every time you throw the dart from the same location you get the same number. Behind the scened p5 is keeping track of where the slips are and where you throw the dart from.
It’s random from the start, but the results are repeatable as long as your seed (friend pinning up the slips) is the same. you can think of this like each of your friends always pins up the slips the same way, so you might get Tera to pin up the slips for one outcome or you might want the seed that Jeff pins up.