0

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

2
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by Falcon25
0

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

1

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.

Votes + Comments
Helpful! thank you
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.