Description
Topic
The p5.Vector functions random2D() and random3D() now use Math.random() to get random numbers.
static random3D() {
const angle = Math.random() * constants.TWO_PI;
const vz = Math.random() * 2 - 1;
const vzBase = Math.sqrt(1 - vz * vz);
const vx = vzBase * Math.cos(angle);
const vy = vzBase * Math.sin(angle);
return new p5.Vector(vx, vy, vz);
}
But for example, the p5.js functions shuffle() and randomGaussian() use p5.js random().
p5.prototype.shuffle = function(arr, bool) {
const isView = ArrayBuffer && ArrayBuffer.isView && ArrayBuffer.isView(arr);
arr = bool || isView ? arr : arr.slice();
let rnd,
tmp,
idx = arr.length;
while (idx > 1) {
rnd = (this.random(0, 1) * idx) | 0;
tmp = arr[--idx];
arr[idx] = arr[rnd];
arr[rnd] = tmp;
}
return arr;
};
The difference between random() and Math.random() is whether they are affected by randomSeed(). With random(), you can get the same set of values if you use the same seed value.
For example, with the following sketch, if you get random3D() values every frame, Math.random() won't work because the values will be different each time.
(latest version)
function setup() {
createCanvas(400, 400, WEBGL);
noStroke();
}
function draw() {
orbitControl();
background(0);
randomSeed(999);
lights();
fill("red");
ambientMaterial("red");
specularMaterial(64);
for(let i=0; i<300; i++){
const v = p5.Vector.random3D();
translate(v.mult(100));
sphere(4);
translate(-v.x,-v.y,-v.z);
}
}
In the following video, the modified random3D() is used in the second half. The same vector can be obtained because it is affected by randomSeed().
2023-05-16.18-47-26.mp4
There is a way to prefetch and use the same set of values, but it is more convenient to have it affected by randomSeed(). I think there is no problem because both methods can be used.
However, I am not very familiar with how to rewrite it, so I would appreciate it if someone could tell me...
static random3D() {
const angle = p5.prototype.random() * constants.TWO_PI;
const vz = p5.prototype.random() * 2 - 1;
const vzBase = Math.sqrt(1 - vz * vz);
const vx = vzBase * Math.cos(angle);
const vy = vzBase * Math.sin(angle);
return new p5.Vector(vx, vy, vz);
}
How should I rewrite Math.random()? random()? p5.random()? p5.prototype.random()? something else...