using System;

public class RollDie
{
   public static void Main( string[] args )
   {
      Random randomNumbers = new Random(); // random number generator
      int[] frequency = new int[ 7 ]; // array of frequency counters

      // roll die 6000 times; use die value as frequency index
      for ( int roll = 1; roll <= 6000; roll++ )
         ++frequency[ randomNumbers.Next( 1, 7 ) ];

      Console.WriteLine( "{0}{1,10}", "Face", "Frequency" );

      // output each array element's value
      for ( int face = 1; face < frequency.Length; face++ )
               Console.WriteLine( "{0,4}{1,10}", face, frequency[ face ] );
   } // end Main

} // end class RollDie

That's my code, what I don't understand if someone can explain to me is how it works. I don't understand the line ++frequency[ randomNumber.Next( 1, 7)]

Does it store 6000 rolls in the frequency[0] and then moves on and stores 6000 rolls in frequency[1]

Basically on each iteration it chooses a random index and increments the value stored in the array at that index by 1. The end result is, you should end up with each item, from index 1 to 6, in the array containing a count of how many times that index was chosen. Because .next is choosing a number from 1-6, 0 will never be used.

Edited 2 Years Ago by tinstaafl

but what does the incrementer do before frequency. I mean if randomNumber.Next rolls a 6, does it increase it to 7, but if thats the case it does not display the 7 in the results

The line actually consists of two actions:
1) randomNumbers.Next( 1, 7 ) - This generates a number from 1 to 6. We'll call this X so it doesn't confuse you in the next part
2) ++frequency[X] - This gets the value at index X. We'll call that value Y. It then adds 1 to Y and stores Y back into frequency[X].

When i was learning C i remember that i wrote down something concerning that problem
and with this writting you ll get a full understanding of it.

example1:

 int y;
 int x = 2;

 y = x++;// or y=x=(x+1) , y stores x imediatlly and THEN x is being incremented y=2 here
 y=x;// if you would go now and write this line you would see that y=3;

 // with preincrement there is nothing much being explained
 // first the value is incremented and thus it is stored in the variable y

 int y;
 y=2;
 y=++x;// y=3

This is a storage problem, if you do ++x and output it and x++ and ouput it youll get same results because you didn't store them anywhere after,, eventualy they increment to +1 . Now just apply this to your array...

Edited 2 Years Ago by castajiz_2: none

I think I got it, please correct my if I misunderstood

  1. a number between 1 and 6 is produced since 7 is not included
  2. frequency[1] stores this number
  3. then the next number is stored in frequency[2] since it increments
  4. When it hits 6 it goes back to 1 and starts all over again.

1.Correct
2-4. doesn't have to be frequency[1] it could be any number between 1,6 depending on the .Next C# integrated function. The function produces a random index. The whole purpose of frequency is to sum all of the occurances of each dice(how many times did a number on the dice appear)

I decided to write the following piece of code in order for you to get a better understanding...

public static void Main(string[] args)
        {
            Random randomNumbers = new Random(); 
            int[] frequency = new int[7];
            int y = 0;
            int[] index = new int[30];


            for (int roll = 0; roll < 30; roll++)
            {
               index[roll] = randomNumbers.Next(1, 7);
               y+= ++frequency[index[roll]];
            }

            Console.WriteLine( "Face"+" "+"Frequency");

            for (int face = 0; face < 7; face++)
            {
                Console.WriteLine(face + "     " + frequency[face]);
            }
            Console.WriteLine("Y value is"+y);

            Console.WriteLine("-------------------------------------------------");
            y = 0;
            for (int i = 0; i < 7; i++) // just setting the values to 0
            {
                frequency[i] = 0;
            }

            for (int roll = 0; roll <30; roll++)
            {
                y+=frequency[index[roll]]++;
            }
            Console.WriteLine("Face" + " " + "Frequency");

            for (int face = 0; face < 7; face++)
            {
                Console.WriteLine(face + "     " + frequency[face]);
            }

            Console.WriteLine("Y value is:"+y);
            Console.ReadLine();
        } 

Answer to your first Question is "It doesn't matter if you use frequency[i]++ OR
you use ++frequency[i] you'll get same results".

I ve made another array to track the random numbers so that you can prove to yourself that preincrement or afterincrement won't change anything. The thing that will change something is setting up a new variable y which when you store the numbers in it will be different (storring the preincrement in y is not the same as storing the afterincrement in y)
You can simplify the code by removing the '+' sign and just making y --> y=frequency[index[roll]]++ and y=++frenquency[index[roll]], this will just get the last value of the array into y when the loop finishes ofcourse. Comparing the two y at the end you'll see that they are different which will return you to my first post in this thread...

I apologize for my slow learning I want to thank everyone that posted. So final rundown to make sure I understand. the ++ before the frequency just adds on everytime the randomNumber is selected for whatever number is produced. So at the end it will keep track of how many 6's have been selected or how many 1's have been selected

Comments
99% correct

I think you got it correct now. But your writting is only a bit incorrect if I may say so.

just adds on everytime the random number is selected.

Yes this is true in one way but the correct sentence would be that it adds everytime no matter the randomnumber. The randomnumber just defines the random index (for example when you throw the dice you don't know what number you will get, that's what the random number does, it gives a random INDEX of the array which actually represents your dice number and then adds the frequency SAYING , OK THIS NUMBER APPEARED SO JUST INCREMENT THE COUNTER OF THAT NUMBER. The counter is actually the frequency and yes you can say that there are 6 counters in your array since for each index you are incrementing a let's say special counter every time the specific number occurs.

Edited 2 Years Ago by castajiz_2: NONE

Comments
Thanks so much
This article has been dead for over six months. Start a new discussion instead.