-2

I am trying to create a column to count how much a number is generated. Here is an example of the output I would like:

Number 1 has been generated 5 times.
Number 2 has been generated 2 times.
etc.

Here is my current code:

Random r = new Random();
        int plus = 0;
        for (int a = 0; a < 10; a++)
        {
            plus++;
            Console.Write("Week {0}: ", plus );
            for (int i = 0; i < 7; i++)
            {
                Console.Write(r.Next(1, 11));
                Console.Write(", ");
            }
            Console.WriteLine();
        }
Stuart Watt
  • 5,242
  • 2
  • 24
  • 31
xabarr
  • 1
  • If you are looking for non school-assignment-acceptable solution check out http://stackoverflow.com/questions/10335223/how-to-build-a-histogram-for-a-list-of-int-in-c-sharp – Alexei Levenkov Mar 06 '15 at 02:31

3 Answers3

3

Use a Dictionary<int,int>, where the key is the random number and the value is the count:

var count = new Dictionary<int, int>();

for (int i = 0; i < 7; i++)
{
    var rndValue = r.Next(1, 11);

    if (count.ContainsKey(rndValue))
        count[rndValue]++;
    else
        count.Add(rndValue, 1);

    Console.Write(rndValue);
    Console.Write(", ");
}

foreach (var c in count)
    Console.WriteLine("Number {0} has been generated {1} time(s).", c.Key, c.Value);

If you want to print the results for any numbers that are generated 0 times, you'll have to add some additional code to make sure a value of 0 is stored in the Dictionary for those values.

Something like this before the foreach statement should work for you:

for (var i = 1; i < 11; i++)
    if (!count.ContainsKey(i))
        count.Add(i, 0);

An alternate solution, using a single-dimensional array, as suggested in the comments. A Dictionary is pretty straight-forward, but this may be even easier to understand.

var count = new int[10];

for (int i = 0; i < 7; i++)
{
    var nextRnd = r.Next(1, 11);
    count[nextRnd - 1]++;

    Console.Write(nextRnd);
    Console.Write(", ");
}

for (var i = 0; i < count.Length; i++)
    Console.WriteLine("Number {0} has been generated {1} time(s).", i + 1, count[i]);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 1
    why not a simple 1-d array? OP is probably just learning, so more simpler data structures might come in handy. – Ilya Ivanov Mar 06 '15 at 02:26
  • 1
    or other way - `GroupBy(n=> n, n=>1).ToDictionary(g=>g.Key,g.Count()) `? – Alexei Levenkov Mar 06 '15 at 02:34
  • 1
    @GrantWinney In terms of code it's a bit simpler, since you only need to do `rndCount[rndValue - 1]++;` where `rndCount` is an `int[]`. Removes the checks you need to do with ContainsKey and populating the dictionary with 0's beforehand. Like you said though, the only caveat is remembering that indexes are zero-based. – Ichabod Clay Mar 06 '15 at 02:46
  • 1
    @GrantWinney I think Ilya's point was that `Dictionary` may not be school-assignment-friendly... `int[12]` would be OK for first couple lessons of C# course, while `Dictionary` is probably sometimes later (but definitely before my `GroupBy` suggestion :) ) – Alexei Levenkov Mar 06 '15 at 02:47
0
var r = new Random();
var weeks = new List<List<int>>(
    Enumerable.Range(0, 10)
              .Select(w => new List<int>(Enumerable.Range(0, 7)
                                                   .Select(i => r.Next(1, 11)))));  

foreach (var week in weeks)
{
    Console.WriteLine("Week {0}: {1}", weeks.IndexOf(week), string.Join(",", week));
}

var allNumbers = weeks.SelectMany(n => n);
foreach (var number in allNumbers.Distinct().OrderBy(n => n))
{
    Console.WriteLine("{0} was generated {1} times", 
                      number,
                      allNumbers.Count(n => n == number));
}
aush
  • 2,108
  • 1
  • 14
  • 24
0

I'd like to using a Dictionary to implement this function:

    Dictionary<int,int> rand_count_list = new Dictionary<int,int>();
    Random r = new Random();
    int plus = 0;
    for (int a = 0; a < 10; a++)
    {
        plus++;
        Console.Write("Week {0}: ", plus);
        for (int i = 0; i < 7; i++)
        {
            int rand = r.Next(1, 11);
            if (rand_count_list.ContainsKey(rand))
                rand_count_list[rand]++;
            else
                rand_count_list[rand] = 1;

            Console.Write(rand);
            Console.Write(", ");
        }
        Console.WriteLine();
    }

    foreach (int key in rand_count_list.Keys)
    {
        Console.Write("Number {0} has been generated {1} times. ", key, rand_count_list[key]);
    }
shijq73
  • 709
  • 8
  • 6