Permalink
Browse files

example updates

  • Loading branch information...
1 parent 5482958 commit d314688dba4fdba0f22b8def6f71f27dad29318b @shiffman committed Jan 5, 2017
View
@@ -4,7 +4,7 @@ authors=[Daniel Shiffman](http://shiffman.net)
url=http://natureofcode.com
sentence=All examples from The Nature of Code book.
paragraph=How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital worlds? This book focuses on the programming strategies and techniques behind computer simulations of natural systems using Processing.
-version=1
+version=2
minVersion=0
maxVersion=0
-prettyVersion=1
+prettyVersion=2
@@ -1,65 +0,0 @@
-// The Nature of Code
-// Daniel Shiffman
-// http://natureofcode.com
-
-float[] vals; // Array to count how often a random # is picked
-float[] norms; // Normalized version of above
-
-void setup() {
- size(200, 200);
- vals = new float[width];
- norms = new float[width];
-}
-
-void draw() {
- background(100);
-
- // Pick a random number between 0 and 1 based on custom probability function
- float n = acceptreject();
-
- // What spot in the array did we pick
- int index = int(n*width);
- vals[index]++;
- stroke(255);
-
- boolean normalization = false;
- float maxy = 0.0;
-
- // Draw graph based on values in norms array
- // If a value is greater than the height, set normalization to true
- for (int x = 0; x < vals.length; x++) {
- line(x, height, x, height-norms[x]);
- if (vals[x] > height) normalization = true;
- if (vals[x] > maxy) maxy = vals[x];
- }
-
- // If normalization is true then normalize to height
- // Otherwise, just copy the info
- for (int x = 0; x < vals.length; x++) {
- if (normalization) norms[x] = (vals[x] / maxy) * (height);
- else norms[x] = vals[x];
- }
-}
-
-// An algorithm for picking a random number based on monte carlo method
-// Here probability is determined by formula y = x
-float acceptreject() {
- // Have we found one yet
- boolean foundone = false;
- int hack = 0; // let's count just so we don't get stuck in an infinite loop by accident
- while (!foundone && hack < 10000) {
- // Pick two random numbers
- float r1 = (float) random(1);
- float r2 = (float) random(1);
- float y = r1*r1; // y = x*x (change for different results)
- // If r2 is valid, we'll use this one
- if (r2 < y) {
- foundone = true;
- return r1;
- }
- hack++;
- }
- // Hack in case we run into a problem (need to improve this)
- return 0;
-}
-
@@ -0,0 +1,52 @@
+// The Nature of Code
+// Daniel Shiffman
+// http://natureofcode.com
+
+// An array to keep track of how often random numbers are picked
+float[] randomCounts;
+
+void setup() {
+ size(800, 200);
+ randomCounts = new float[20];
+}
+
+void draw() {
+ background(255);
+
+ // Pick a random number and increase the count
+ int index = int(acceptreject()*randomCounts.length);
+ randomCounts[index]++;
+
+ // Draw a rectangle to graph results
+ stroke(0);
+ strokeWeight(2);
+ fill(127);
+
+ int w = width/randomCounts.length;
+
+ for (int x = 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 = x
+float acceptreject() {
+ // Have we found one yet
+ boolean foundone = false;
+ int hack = 0; // let's count just so we don't get stuck in an infinite loop by accident
+ while (!foundone && hack < 10000) {
+ // Pick two random numbers
+ float r1 = (float) random(1);
+ float r2 = (float) random(1);
+ float y = r1*r1; // y = x*x (change for different results)
+ // If r2 is valid, we'll use this one
+ if (r2 < y) {
+ foundone = true;
+ return r1;
+ }
+ hack++;
+ }
+ // Hack in case we run into a problem (need to improve this)
+ return 0;
+}

0 comments on commit d314688

Please sign in to comment.