Member Avatar for Falcon25

Hey everyone. This is my code at the moment:

namespace Experimenting
{
    class Program
    {
        const int maximum = 5;

        static int[] myArray = new int[maximum];

        static void Main(string[] args)
        {
            RandomNumbers();
            ScreenOutput();
        }

        static void RandomNumbers()
        {
            int x = 0;
            
            for (int i = 0; i < maximum; i++)
            {
                Random numbers = new Random(i);
                x = numbers.Next(0,100);
                Console.WriteLine(x);
                myArray[i] = x;
            }
            
            Console.ReadLine();
            return;
        }

        static void ScreenOutput()
        {
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("Here are five numbers. What is the sum of all these numbers?");
            Console.ReadLine();
        }
    }
}

I was wondering if anyone knew how to put a visible timer on the screen that ticks down whilst the user is trying to add up all the numbers? Once the timer finishes I wanted it to display a message or go onto another method. If anyone can help it'l be great. Also, quick question, do you guys use enumerations alot? My book has gone onto learning about these but I can't really grasp the concept of it. Thank you :D

Member Avatar for Falcon25

Hey everyone. This is my code at the moment:

namespace Experimenting
{
    class Program
    {
        const int maximum = 5;

        static int[] myArray = new int[maximum];

        static void Main(string[] args)
        {
            RandomNumbers();
            ScreenOutput();
        }

        static void RandomNumbers()
        {
            int x = 0;
            
            for (int i = 0; i < maximum; i++)
            {
                Random numbers = new Random(i);
                x = numbers.Next(0,100);
                Console.WriteLine(x);
                myArray[i] = x;
            }
            
            Console.ReadLine();
            return;
        }

        static void ScreenOutput()
        {
            Console.WriteLine("-------------------------------------");
            Console.WriteLine("Here are five numbers. What is the sum of all these numbers?");
            Console.ReadLine();
        }
    }
}

I was wondering if anyone knew how to put a visible timer on the screen that ticks down whilst the user is trying to add up all the numbers? Once the timer finishes I wanted it to display a message or go onto another method. If anyone can help it'l be great. Also, quick question, do you guys use enumerations alot? My book has gone onto learning about these but I can't really grasp the concept of it. Thank you :D

I've just realised this code doesn't work, it keeps throwing out the same random numbers. Back to the drawing board I guess

Don't declare Random objects in methods unless that method is only ever called once (or very rarely). You've placed it inside your loop so it creates a new random generator each time through the loop. Since the generator is seeded by the clock, there isn't enough time between instantiations for the clock to change so you get the same generator over and over again (thus the same numbers). Move line 21 to line 18.

commented: Helpful! thank you +1
Member Avatar for Falcon25

Ahh thanks for the advice Momerath I'l keep it in mind in my future code, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.