Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Calculating an average in Java.. having some issues with function
New Posts  All Forums:Forum Nav:

Calculating an average in Java.. having some issues with function - Page 2

post #11 of 15
I was going to say what CBR beat me to. Adding a print statement in your loop to print the different variables will tell you if the other functions are indeed working correctly and you are getting valid die rolls. Once you do that I would definitely move back to a for loop if you are limiting to 1000 rolls (will cut down on your code).

Your average (as stated already) should be += not =+
I'm Back
(19 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1055T @3.71 Asus M4A785TD-V EVO Gigabyte GTX 460 1GB 16 GB Corsair XMS 3 
Hard DriveHard DriveHard DriveHard Drive
Kingston V300 SSD Western Digital Caviar Samsung Spinpoint Western Digital Caviar black 
Optical DriveCoolingOSMonitor
Sony CD DVD R/WR Prolimatech Megahalems  Windows 7 x64 Samsung Smart TV 
MonitorKeyboardPowerCase
Hanns G POS Saitek Eclipse II Corsair 750 TX NZXT Source 210 
Mouse
Logitech G700S 
  hide details  
Reply
I'm Back
(19 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X6 1055T @3.71 Asus M4A785TD-V EVO Gigabyte GTX 460 1GB 16 GB Corsair XMS 3 
Hard DriveHard DriveHard DriveHard Drive
Kingston V300 SSD Western Digital Caviar Samsung Spinpoint Western Digital Caviar black 
Optical DriveCoolingOSMonitor
Sony CD DVD R/WR Prolimatech Megahalems  Windows 7 x64 Samsung Smart TV 
MonitorKeyboardPowerCase
Hanns G POS Saitek Eclipse II Corsair 750 TX NZXT Source 210 
Mouse
Logitech G700S 
  hide details  
Reply
post #12 of 15
I did something like this...average hovers right around 7, which is expected.
Code:
import java.util.Random;

public class dice{
   public static void main(String args[]){
    
    Random r = new Random();
    double average;
    double sum = 0;

    for(int i = 0; i < 100000; ++i){
    
        sum += (r.nextInt(6) + 1) + (r.nextInt(6) + 1);
    }
    
    average = sum / 100000;
    System.out.println("Sum is: " + sum);
    System.out.println("Average is: " + average);
   
    }
}
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #13 of 15
Thread Starter 
Quote:
Originally Posted by surfbumb View Post

I did something like this...average hovers right around 7, which is expected.
Code:
import java.util.Random;
public class dice{
   public static void main(String args[]){
    
    Random r = new Random();
    double average;
    double sum = 0;
    for(int i = 0; i < 100000; ++i){
    
        sum += (r.nextInt(6) + 1) + (r.nextInt(6) + 1);
    }
    
    average = sum / 100000;
    System.out.println("Sum is: " + sum);
    System.out.println("Average is: " + average);
   
    }
}


EDIT: Figured it out, I used my code but used += instead of =+.

I think that code works but my sum is always exactly even.
Code:
public static void Average() {
                PairOfDice pod = new PairOfDice();
                int average = 0;
                int sum = 0;

                for(int i = 0; i < 100000; ++i){
                    
                        sum += pod.getDie1() + pod.getDie2();
            }
            
            
            System.out.println("Sum is: " + sum);
            average = sum / 100000;
            System.out.println("Average is: " + average);

        }
Quote:
It took 2 roll(s) to get a double.
166470 doubles were rolled.
Sum is: 1100000
Average is: 11
Quote:
It took 2 roll(s) to get a double.
165809 doubles were rolled.
Sum is: 300000
Average is: 3

Edited by skatingrocker17 - 1/20/12 at 5:36pm
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #14 of 15
After 3 runs this is what my output looks like:
Code:
Sum is: 701314.0
Average is: 7.01314
Sum is: 699767.0
Average is: 6.99767
Sum is: 700672.0
Average is: 7.00672


I'd recommend using the Random library in your getDie() method. Can we see your getDie() method? That would seem to be where your troubles are at.
Edited by surfbumb - 1/20/12 at 6:26pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #15 of 15
You are using ints so you will always get whole numbers. Use a float instead.

And I told you about that =+ thing!
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Calculating an average in Java.. having some issues with function