• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Very strange bug, Experts wanted

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is very very strange, with this code and the jdk1.3
no matter how many times the dice rolls the 4th face
always is the highest number. I tried 10,000, 1 million,
1000 etc.... and the 4th face is always the highest number.
////////////////////////
import javax.swing.*;
public class Dice {
public static void main(String args[])
{

int freq1 = 0, freq2 =0,
freq3 = 0, freq4 = 0,
freq5 = 0, freq6 =0, face;
for( int roll=1; roll <1000; roll++) {
face = 1+ ( int ) ( Math.random() * 6);
switch ( face) {
case 1:

++freq1;
break;
case 2:

++freq2;
break;
case 3:
++freq3;
case 4:
++freq4;
break;
case 5:
++freq5;
break;
case 6:
++freq6;
break;

}
}
JTextArea outputArea = new JTextArea( 16,22 );
outputArea.setText(
"Face\tFreq" +
"\n1\t" + freq1 +
"\n2\t" + freq2 +
"\n3\t" + freq3 +
"\n4\t" + freq4 +
"\n5\t" + freq5 +
"\n6\t" + freq6 );

JOptionPane.showMessageDialog( null, outputArea,
"Rolling Dice 1 thousand times",
JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You left a break out of case 3 :
freq4 counts both 3 and 4
tch tch!
Bill
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bill! Being in front of a computer monitor for a few hours
strains my eye balls.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic