We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Re: Processing project... coin flip
Page Index Toggle Pages: 1
Re: Processing project... coin flip (Read 1218 times)
Re: Processing project... coin flip
Apr 24th, 2010, 9:52pm
 
Hi,

There were a few errors in your code.
first of all you were using the same variable (numHeads) for the for loop and for counting the results. This caused numHeads to be reused inside the loop and miscount. Note that I reset numHeads to 0 at the beginning of each trial.

The second thing is that in order to increment a variable you should either use
x = x + 1
or
x++
but not both (i.e. x = x++);

You should also read a little about arrays as it will make your life easier.

anyway here's the working code:


int zeroOfTen;
int oneOfTen ;
int twoOfTen;
int threeOfTen;
int four0fTen;
int fiveOfTen;
int sixOfTen;
int sevenOfTen;
int eightOfTen;
int nineOfTen;
int tenOfTen;

int numHeads;
int trialNumber;
int flipNumber;


void setup ()
{
size (400,400);
background (10,100,25);
simulate ( ) ;
println ( "Done simulating" ) ;
drawGraph ( ) ;
noLoop ( ) ;
}


void simulate ( )
{

for(trialNumber=0;trialNumber<1000;trialNumber++)
{
  numHeads = 0;
  for (int i=0;i<10;i++)
  {
 
  Boolean isHeads = false;
  isHeads = (random (0,1) > 0.5);
   if (isHeads)
     numHeads++;
  }

  switch(numHeads){
  case 0:
    zeroOfTen++;
    break;
  case 1:
    oneOfTen++;
    break;
  case 2:
    twoOfTen++;
    break;
  case 3:
    threeOfTen ++;
    break;
  case 4:
    four0fTen ++;
    break;
  case 5:
    fiveOfTen ++;
    break;
  case 6:
    sixOfTen ++;
    break;
  case 7:
    sevenOfTen ++;
    break;
  case 8:
    eightOfTen ++;
    break;
  case 9:
    nineOfTen ++;
    break;
  case 10:
    tenOfTen ++;
    break;

  }
}
}

void drawGraph ( )
{

for(flipNumber=1;flipNumber<10;flipNumber++)
{
  fill(0,0,50);
  rect(0,50,zeroOfTen,20);
  rect(0,80,oneOfTen,20);
  rect(0,110,twoOfTen,20);
  rect(0,140,threeOfTen,20);
  rect(0,170,four0fTen,20);
  rect(0,200,fiveOfTen,20);
  rect(0,230,sixOfTen,20);
  rect(0,260,sevenOfTen,20);
  rect(0,290,eightOfTen,20);
  rect(0,320,nineOfTen,20);
  rect(0,350,tenOfTen,20);
}
}

Re: Processing project... coin flip
Reply #1 - Apr 25th, 2010, 12:32am
 
Some additional remarks to the good ones of guyy:
- As advised, start to use arrays... Smiley
- numHeads doesn't need to be global. Declare it where it is initialized to zero.
- Use boolean, not Boolean. The first one is a primitive type, small and fast, the second one is an object, much bigger, slower and that must be garbage collected, which is significant when created in a loop.
- The loop in the drawGraph() function isn't necessary. It works as well when you comment out the for loop.
Re: Processing project... coin flip
Reply #2 - Apr 25th, 2010, 6:12am
 
Thank you both...  that makes sense.  I am definately going to hit the books on arrays and Boolean.  You guys rock!  Cheesy
Re: Processing project... coin flip
Reply #3 - Apr 25th, 2010, 9:48am
 
@PhiLho: The part of boolean/Boolean was new to me. Could you elaborate? What is the "advantage" or aim of the Boolean object?
Re: Processing project... coin flip
Reply #4 - Apr 25th, 2010, 9:59am
 
Actually, the Boolean object isn't very useful... Smiley
But well, it can be used when you have to provide an object, not a primitive value. For example, when you need to put them in a collection (ArrayList or similar).
Page Index Toggle Pages: 1