1 / 21

The Princeton Egg

The Princeton Egg. The Global Consciousness Project (video) Princeton Egg Website Our Egg:   PrincetonEgg.cpp. a few things will help us design our own Princeton Egg. a coin flipper. "for" loop boundaries. // int x is the for loop variable counting // coin flips: int x = 0;

kare
Download Presentation

The Princeton Egg

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Princeton Egg The Global Consciousness Project (video) Princeton Egg Website Our Egg:   PrincetonEgg.cpp

  2. a few things will help us design our own Princeton Egg a coin flipper

  3. "for" loop boundaries // int x is the for loop variable counting // coin flips: int x = 0; for (x=1; x<=100; x=x+1) { cout << "X in for loop: " << x << endl; } cout << "X after for loop: " << x << endl;

  4. Domain of a variable for (int x=1; x<=100; x=x+1) { cout << "X in for loop: " << x << endl; } cout << "X after for loop: " << x << endl; // error, x not useable outside for loop

  5. #include <cstdlib> rand( ); - "pseudo" random number randomNumber = rand( )%10; // generate random number 0 thru 9 srand( int seed ); - seed the random number

  6. remember...calling a library function int ranNum; ranNum = rand( ) %2; // random one or zero The result of the library function is placed into ranNum. The rand()%2 function returns a random 0 or 1 as a “service”.

  7. getting a coin flip int seed = 0; int randomNumber = 0; cout << "type a number to seed generator"; cin >> seed; srand( seed ); randomNumber = rand( )%2; // 0 or 1

  8. two problems with rand( ) 1. random numbers are the same every time you run it (the algorithm starts at a consistant place) 2. rand( )%2 gives equally spaced 0s and 1s (not random for these low numbers)

  9. #include <ctime> • time( 0 ); - returns the number of seconds since Jan 1 1970. I have no idea why. But, not an integer, some mutant "time" string… • so: #include <ctime> #include <cstdlib> srand ( (int) time(0) ); // seed random number generator with time

  10. actually, time(0) is a "structure" • a structure, or "struct" in C++, is a meta-variable, that contains many variables… one main, and a number of auxiliary values • the "time" struct contains the number of seconds (its main value), but also month, day, date and year which we can access when we get good at this

  11. #include <iostream> #include <string > #include <math.h> #include <cstdlib> #include <ctime> using namespace std; int main( ) {

  12. Our Princeton Egg Elementary Statistics

  13. Design 1. Flip some coins: a lot of them, in a for loop - use rand to generate numbers 0-9. - 0-4 are heads, 5-9 are tails 2. Count the number of heads and the number of tails

  14. Design 3. abs(heads - tails) / 2 is the number of errant coin flips e.g. For 10 flips, if you get 6 heads and 4 tails, 6 - 4 / 2 = 1 coin flip was errant NOTE THAT THIS INVOLVES DIVIDING INTEGERS

  15. we will use Casting ints and doubles ARE interchangeable but we have to cast int x; double y; x = 10; y = (double)x;

  16. casting int numberOfFlips=0; int numberOfHeads=0; int numberOfTails=0; int diff = 0; // mistake double percentage = 0.0; diff = abs( numberOfHeads - numberOfTails )/2; // no division should EVER be done with integers

  17. best: all divisions done with doubles double diff = 0.0; diff = (double) (abs( numberOfHeads - numberOfTails )) /2.0; 4. Calculate total % of errant flips percentage = (diff/(double)(numberOfFlips)) * 100.0;

  18. Design 5. Finally, is percentage greater than 10%? - stay home

More Related