Upload
richard-brown
View
229
Download
1
Embed Size (px)
Citation preview
The Princeton Egg
The Global Consciousness Project (video)
Princeton Egg WebsiteOur Egg: PrincetonEgg.cpp
a few things will help a few things will help us design our own us design our own
Princeton EggPrinceton Egg
a few things will help a few things will help us design our own us design our own
Princeton EggPrinceton Egg
a coin flippera coin flipper
"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;
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
#include <cstdlib>
rand( ); - "pseudo" random number
randomNumber = rand( )%10; // generate random number 0 thru 9
srand( int seed ); - seed the random number
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”.
two problems with rand( )
1. random numbers are the same every time you run it (the algorithm starts at a consistent place)
2. rand( )%2 gives equally spaced 0s and 1s (not random for these low numbers) on old computers (32 bit)
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
#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
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
#include <iostream>#include <string >#include <math.h>#include <cstdlib>#include <ctime>using namespace std;
int main( ) {
Our Princeton EggOur Princeton EggOur Princeton EggOur Princeton Egg
Elementary StatisticsElementary Statistics
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
Design
3. abs(heads - tails) / 2 is the number of errant coin flipse.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
we will use Casting
ints and doubles ARE interchangeable but we have to cast
int x;double y;
x = 10;y = (double)x;
casting
int numberOfFlips=0;int numberOfHeads=0; int numberOfTails=0;int diff = 0; // mistakedouble percentage = 0.0;
diff = abs( numberOfHeads - numberOfTails )/2;// no division should EVER be done with integers
best: all divisions done with doubles
double diff = 0.0;diff = (double) (abs( numberOfHeads - numberOfTails )) /2.0;
4. Calculate total % of errant flipspercentage =
(diff/(double)(numberOfFlips)) * 100.0;