Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# C# Homework Help

6 replies to this topic

### #1 Talonaces

Talonaces

• Members
• 51 posts
• OFFLINE
•
• Local time:02:14 PM

Posted 27 May 2008 - 09:16 PM

I just finished up my course in C#. I got a B in the course because I could not complete the following assignment:

We are going to see how uniform the random number generator is for .NET. You will first build a .NET pseudorandom number generator using the following code:
Random r = new Random();

To generate a random number between 0 and 24, use this code:
r.Next(25);

We will be taking a histogram of the values generated by the random number generator. To do this, build an array of 25 integer values, and make sure they are all equal to zero.

Then, in a loop that runs a lot of times, like at least 1000, generate a random number:

int index = r.Next(25);

This integer named index then becomes the index of the integer in your array that you will increment. In other words, by using an array of 25 values indexed 0-24, you are keeping 25 separate counters of occurrences of the index number.

After you have collected your data, print out the counts that you got. The numbers should be pretty close to one another in value. Or, if you want to show off, use the star printing technique we learned in class to print out a sideways graph, like this:

0: ********************* (4 %)
1: ********************* (4 %)
2: ********************** (5 %)
3: ********************* (3 %)

and so on.

OK. Here is my question. This has frustrated me for about 2 weeks now, and even though the course is over, I still want to know how to do this. I missed two classes due to a death in the family and I never fully caught up with the rest of the class. Could someone please show me the solution and give me some explanation of how the code works? All I have completed so for is getting the program to produce the random arrays.

### #2 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:12:14 PM

Posted 27 May 2008 - 09:33 PM

I would be glad to help you, but if you want to learn, you will write the code yourself. Show me the code you have now, and what you think should be next, and I be happy to help you figure it out.

### #3 Talonaces

Talonaces
• Topic Starter

• Members
• 51 posts
• OFFLINE
•
• Local time:02:14 PM

Posted 27 May 2008 - 09:56 PM

Here is what I have so far. It is all scrambled up, due to some friends trying to help me. To tell you the truth, I have a hard time understanding the code. C++ was much easier for me to understand.
```static void Main(string[] args)
{
int total = 0;
int mean = 0;
Random r = new Random();
int[] junk = new int[25];
int[] count = new int [25];
int index = 0;

//for (int a = 0; a < 10; a++)
//{
for (int i = 0; i < 250; i++)
{
junk[i] = r.Next(25);
index = r.Next(25);
if (index > 0)
count[i]++;
//Console.WriteLine("{0}:{1}", i, r.Next(25));
Console.WriteLine("Index Value is {0}",count[index]);
}
//Console.WriteLine("Index Value is {0}", count[index]);
//}

Due to the help I received, I am much lost at this point.

Thanks!

### #4 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:12:14 PM

Posted 27 May 2008 - 10:22 PM

I added code tags to make it a bit easier to read. Anyway, let's think about this logically. What you are really trying to create is called a bucket array. So the first think you need to do is create your array. You already have some code like that:
int[] junk = new int[25];
Except that junk is a bad name for an array because it doesn't tell you anything about the contents of the array. Yeah, I realize this is a tiny program, but you should still take the time to use good names. The next task is to make sure that each element of the array is set to zero. For now, just create a small for loop that does that. By default, arrays are indexed to zero, so you probably do nat have to do this, but for safety sake, you should do it anyway. Not all languages are as thoughtful, and the array may be filled with junk.

After that, instantiate Random. You already know how to do that also.

Now, create another for loop, except this time let's have it loop 10000 times (or 100,000 times). It will only take a few seconds to run. Now inside this loop, get a random number. It is going to be a number from 0-24.Coincidentally, you have an array that is indexed from 0-24. Now here's where the fun is. If the random number is 6, then you need to add 1 to the array at index 6. If array[6] was zero before, now it will be one. For each number that is generated, use that number as the index in the array, and increase the count at that index by one (for example array[6]++;). Your loop only needs one line of code inside it, but you might want to do it with two, especially if you are not used to chaining methods.

Now that loop is finished, you only need to loop through the array one more time, and print out the totals at each of the indexes.

### #5 Talonaces

Talonaces
• Topic Starter

• Members
• 51 posts
• OFFLINE
•
• Local time:02:14 PM

Posted 27 May 2008 - 11:50 PM

Ok. I think I got through to Instantiating Random. Now is where I get lost, or to be more exact, don't know what to do. Here is what I have so far:

`static void Main(string[] args)        {            Random r = new Random();            int[] myarray = new int[25];            for (int i = 0; i < 25; i++)            {                myarray[i] = 0;            }            for (int i = 0; i < 25; i++)            {                myarray[i] = r.Next(25);                Console.WriteLine("{0}: {1}", i, myarray[i]);            }                                    Console.ReadKey();         }`

I can't figure out how to start the next loop to get what I am looking for.

### #6 Talonaces

Talonaces
• Topic Starter

• Members
• 51 posts
• OFFLINE
•
• Local time:02:14 PM

Posted 28 May 2008 - 12:33 AM

I got a little further. I have a nested for loop to 10000 sets of 24 random integers. The noted out code is where I am trying to index the intergers. I think I need an if statement inside the last for loop to do this but can't wrap my head around. Am I close? Here is what I have.

`static void Main(string[] args)        {            Random r = new Random();            int index = 0;            int[] myarray = new int[25];            for (int i = 0; i < 25; i++)            {                myarray[i] = 0;            }            for (int a = 0; a < 10000; a++)            {                for (int i = 0; i < 25; i++)                {                    myarray[i] = r.Next(25);                    Console.WriteLine("{0}: {1}", i, myarray[i]);                }            }            /*for (int i = 0; i < 25; i++)            {                myarray[i] = r.Next(25);                index = r.Next(25);                Console.WriteLine("{0}: {1}", i, index);            }*/            Console.ReadKey();        }`

How do I structure that last for loop?

### #7 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:12:14 PM

Posted 28 May 2008 - 07:41 AM

There is no need for a nested loop at all. This is correct:
```Random r = new Random();
int[] myarray = new int[25];
for (int i = 0; i < 25; i++)
{
myarray[i] = 0;
}```

The second loop just needs to populate the array with random numbers.
```for (int a = 0; a < 10000; a++)
{
}```
This part is correct from your last post. Here is where you are getting lost, so I will try to explain a bit more clearly. We will worry about the final loop later. Again, there are no nested loops. I'll try a smaller example. Lets look at a lame graphic of an array:

|0|0|0|0|0|0|

Here is an array of 5 elements. We have already filled it with zeros. Now we go into our loop where we want to count how many times each number comes up. In this case though, we are only going to be generating 0-5 though, since that is all the bigger the array is. So in our loop, let's generate a random number, and let us say it is a 3. So since we saw a three, we need to tell the array that we saw a three so that it can store the value. We use three as the index for the array, and increment the array at index three by one. Now our array looks like this:

|0|0|0|1|0|0|

Going back into the loop the second time, we generate a two, so we increment the array at index two. Now our array looks like this:
|0|0|1|1|0|0|

Then maybe we saw a three again:
|0|0|1|2|0|0|

Let's pretend for now that we only did our loop 10 times, and maybe the array looks like this when we are done:
|2|1|1|2|3|1|

What is this telling us? It tells us the following:
We saw 0 two times.
We saw 1 once.
We saw 2 once.
We saw 3 twice.
We saw 4 three times.
We saw 5 once.

*************************

Another way to look at it is to pretend that you are walking around, and you just happen to be carrying around 6 buckets. A truck carrying numbers from 1-5 tips over, and spills its contents all over the road. You being the good person decide to help clean up, but you also want to keep the numbers separated so that the truck driver knows all of his numbers were recovered. As you go along, you put all of the ones in a bucket, all of the twos in another bucket, threes in another, etc. When you are done cleaning up, all you need to do is look in each bucket to see how many of each that you have.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users