Regarding the p5.Vector functions random2D() and random3D() using Math.random()
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 24k
- Forks
- 3.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 25
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().
https://github.com/processing/p5.js/assets/39549290/f5d70911-80af-4545-a36c-d830d9af2a8f
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...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/math/p5.Vector.js at random2D() and random3D(), then compare their use of Math.random() with shuffle() and randomGaussian(). Verify whether randomSeed(999) should make repeated vector generation deterministic, using the sketch in the issue; done means the intended random-source behavior is consistent and documented by the relevant checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100