You are not logged in.

#1 2009-03-16 03:08:46

Raccoon1400
Member
From: Ontario, Canada
Registered: 2008-04-14
Posts: 853

The weirdest programming issue ever[SOLVED,but still doesnt make sense

using C#
I am writing a program that rolls a specified number of dice many times, and prints the % it gets each total sum.
I am compiling with mono.
I change something that should have no effect and it messes up the entire program.
I change these lines:
from

int numberDice = 2;

output:
the first number is the sum of the die. The second is the % each number is rolled.

Enter the number of dice to roll
Enter the number of sides the dice have

2    2.746%
3    5.378%
4    8.179%
5    11.098%
6    13.698%
7    16.678%
8    13.99%
9    11.335%
10    8.488%
11    5.599%
12    2.811%

new line

int numberDice = int.Parse(Console.ReadLine());

output:
it doesn't even print the sum of the die, even though it's printed by the same line as the %.

Enter the number of dice to roll
2
Enter the number of sides the dice have

22.633%
35.614%
48.394%
511.207%
613.725%
716.616%
813.985%
911.147%
108.334%
115.497%
122.848%

And this is even weirder. I use the working lines, but add this line:

int testvar = int.Parse(Console.ReadLine());

and never use testvar in the program, and I still get the bad output.

using System;
using System.Collections.Generic;
using System.Text;

namespace Dice
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number of dice to roll");
            int numberDice = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the number of sides the dice have");
            int diceSides =6;// int.Parse(Console.ReadLine());
        //Console.WriteLine("{0}, {1}", numberDice, diceSides);
        Console.WriteLine();
        double percent;
            int[] countArray = new int[diceSides*numberDice+1];
            foreach (int n in countArray)
               countArray[n] = 0;
            int total=0;
            int rolls = 100000;
            Random rand = new Random();
            for (int i = 0; i < rolls; i++)
            {
                total = 0;
                total = (rand.Next() % diceSides)+1;
        for (int loop=1; loop<=numberDice-1; loop++)
            {
            total = total + (rand.Next() % diceSides)+1;
        }
                for (int count =0; count<=(diceSides*numberDice); count++)
                {
                    if (total == count)
                    {
                        countArray[count] = countArray[count] + 1;
                    }
                }
            }
        double rollsDec = rolls;
        for (int loop2 =1; loop2<=(diceSides*numberDice); loop2++)
        {
            double counter= countArray[loop2];
            if (countArray[loop2] != 0)
            {
                percent = (counter/rollsDec)*100;
                Console.WriteLine("{0}    {1}%",loop2, percent);
            }
        }
            //Console.ReadKey();
        }
    }
}

Last edited by Raccoon1400 (2009-03-17 02:23:26)


Fustrated Windows users have two options.
1. Resort to the throwing of computers out of windows.
2. Resort to the throwing of windows out of computers.

Offline

#2 2009-03-17 01:14:11

jsgt
Member
Registered: 2009-03-16
Posts: 29

Re: The weirdest programming issue ever[SOLVED,but still doesnt make sense

The sums of the die do get printed, but the spaces between the sums and percentages are for some reason deleted when you use readline. Just an observation... maybe it'll be of some use in your debugging.

Offline

#3 2009-03-17 02:11:32

Raccoon1400
Member
From: Ontario, Canada
Registered: 2008-04-14
Posts: 853

Re: The weirdest programming issue ever[SOLVED,but still doesnt make sense

jsgt wrote:

The sums of the die do get printed, but the spaces between the sums and percentages are for some reason deleted when you use readline. Just an observation... maybe it'll be of some use in your debugging.

The percentages are printed, it's the sum of the die that doesn't.


Fustrated Windows users have two options.
1. Resort to the throwing of computers out of windows.
2. Resort to the throwing of windows out of computers.

Offline

#4 2009-03-17 02:14:43

Raccoon1400
Member
From: Ontario, Canada
Registered: 2008-04-14
Posts: 853

Re: The weirdest programming issue ever[SOLVED,but still doesnt make sense

Here's something interesting. I get it to write the sum of the die, the number of times it rolled that sum, and the number of total rolls
then below it writes the percentage. Look at the sum, the number of times it got that sum, then look at the percent below.
loop 12 2754 100000
122.754%


Fustrated Windows users have two options.
1. Resort to the throwing of computers out of windows.
2. Resort to the throwing of windows out of computers.

Offline

#5 2009-03-17 02:21:48

Raccoon1400
Member
From: Ontario, Canada
Registered: 2008-04-14
Posts: 853

Re: The weirdest programming issue ever[SOLVED,but still doesnt make sense

I seem to have fixed it. All I did was delete and retype these lines:
                percent = (counter/rollsDec)*100;
                Console.WriteLine("{0}    {1}%", loop2, percent);
Though the new lines are identical to the original ones.


Fustrated Windows users have two options.
1. Resort to the throwing of computers out of windows.
2. Resort to the throwing of windows out of computers.

Offline

Board footer

Powered by FluxBB