You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
Hi, I'm following the youtube playlist on nature of code, and I didn't found the corresponding p5.js code for the Custom Distribution video (https://www.youtube.com/watch?v=DfziDXHYoik).
So here is the code I created, based on the processing equivalent:
// An array to keep track of how often random numbers are pickedletrandomCounts=[];functionsetup(){background(200);createCanvas(800,200);for(vari=1;i<=20;i++){randomCounts.push(0);}}functiondraw(){background(255);// Pick a random number and increase the countvarindex=int(acceptreject()*randomCounts.length);randomCounts[index]++;// Draw a rectangle to graph resultsstroke(0);strokeWeight(2);fill(127);varw=width/randomCounts.length;for(varx=0;x<randomCounts.length;x++){rect(x*w,height-randomCounts[x],w-1,randomCounts[x]);}}// An algorithm for picking a random number based on monte carlo method// Here probability is determined by formula y = xfunctionacceptreject(){// Have we found one yetvarfoundone=false;varhack=0;// let's count just so we don't get stuck in an infinite loop by accidentwhile(!foundone&&hack<10000){// Pick two random numbersvarr1=float(random(1));varr2=float(random(1));vary=r1*r1;// y = x*x (change for different results)// If r2 is valid, we'll use this oneif(r2<y){foundone=true;returnr1;}hack++;}// Hack in case we run into a problem (need to improve this)return0;}
Hopefully you can update your repository and add this code example too.
Cheers.
The text was updated successfully, but these errors were encountered:
Hi, I'm following the youtube playlist on nature of code, and I didn't found the corresponding p5.js code for the Custom Distribution video (https://www.youtube.com/watch?v=DfziDXHYoik).
So here is the code I created, based on the processing equivalent:
Hopefully you can update your repository and add this code example too.
Cheers.
The text was updated successfully, but these errors were encountered: