Click here to Skip to main content
15,995,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First of all, I would like to thank Code Project for helping me out in my quest to learn C#. I started learning to the programme as a hobby and picked c# as I heard that it is quite easy to learn for a person without any programming background. However, I realised that it is still too tough for me. But Code Project has always helped me out in figuring things out. Anyways...

So I was trying to write a C# console app that displays animation on the console. So far I'm able to write about 95% of the code and it is working but I'm still not able to figure out how to generate a random number from list of number only once. I have included the code I have written for everyone to review. Right now I have to use a redundant method for making sure that all the characters are printed on the console. Which consumes a lot of time and i guess memory too. Below is my code. Please excuse me if the code seems hard to read as It is been only about 1.5months since when i started with c#.

If anyone can suggest me what I can do to remove the redundant Iteration number from my code then I guess it will be complete.

C#
<pre>
using System;
using System.IO;
using System.Threading;

namespace BeautifulNameAnimation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please play in maximised window mode. PRESS ENTER after maximising your window");
            Console.ReadKey();
            Console.WriteLine("Please enter the complete path of your text file including the text file name with extension: ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(">>> ");
            Console.ResetColor();
            string path = Console.ReadLine();
            Console.WriteLine("Provide animation delay number. Bigger the number Slower the animation.");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(">>> ");
            Console.ResetColor();
            int animDelay = int.Parse(Console.ReadLine());
            Console.WriteLine("Redundancy Number: (This number decides the iteration for animation) -- Start with 1000 and then gradually increase it. ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(">>> ");
            Console.ResetColor();
            int redundencyCounter = int.Parse(Console.ReadLine());
            Console.Clear();

            //// Program Execution Stage -1
            int[] Method1Output = ReturnColsAndRows(path);
            //// Program Execution Stage -2
            char[,] Method2Output = ExtractAllChars(Method1Output, path);
            //// Program Execution Stage -3 (Final OutPut)
            PrintTextFile(Method2Output, animDelay, redundencyCounter);
            Console.ResetColor();
            Console.ReadKey();
        }
        //Method No. 1
        #region Method for calculating Rows and Colums in the Source Text File.
        public static int[] ReturnColsAndRows(string path)
        {
            StreamReader reader = new StreamReader(path);
            int cols = reader.ReadLine().Length;
            int rows = 1;
            string line = reader.ReadLine();
            while (line != null)
            {
                rows++;
                line = reader.ReadLine();
            }
            reader.Close();
            int[] rowsNcols = new int[2] { rows, cols }; //To be used in Method No. 2 for Char[,]
            return rowsNcols;
        }
        #endregion
        //Method No. 2
        #region Method for extracting individual characters from Source Text File.
        public static char[,] ExtractAllChars(int[] Method1Output, string path)
        {
            StreamReader reader = new StreamReader(path);
            string line = reader.ReadLine();
            char[,] charsInSourceFile = new char[Method1Output[0], Method1Output[1]];
            for (int row = 0; row < charsInSourceFile.GetLength(0); row++)
            {
                for (int col = 0; col < charsInSourceFile.GetLength(1); col++)
                {
                    charsInSourceFile[row, col] = line[col];
                }
                line = reader.ReadLine();
            }
            reader.Close();
            return charsInSourceFile;
        }
        #endregion
        //Method No. 3
        #region Method for printing extracted chars in a random fashion
        public static void PrintTextFile(char[,] charsInSourceFile, int animDelay, int redundencyCounter)
        {
            int counter = 0;
            Random rand = new Random();
            while (true)
            {
                int row = rand.Next(charsInSourceFile.GetLength(0));
                int col = rand.Next(charsInSourceFile.GetLength(1));
                Console.SetCursorPosition(col, row);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(charsInSourceFile[row, col]);
                counter++;
                Thread.Sleep(animDelay);
                if (counter == redundencyCounter) // This is the problem
                {
                    Console.SetCursorPosition(0, charsInSourceFile.GetLength(0) + 2);
                    break;
                }
            }
        }
        #endregion
    }
}



What I have tried:

I tried to google my question by not able to find any solution
Posted
Updated 16-Oct-18 18:12pm
Comments
BillWoodruff 19-Oct-18 15:47pm    
Your code assumes all lines in the file are the same length: is that what you really want ?

If the lines are all the same length, and you use the same file every time, then you can generate the random points and characters once, and shuffle them if you wish the animation to be different each time.

1 solution

Quote:
I'm still not able to figure out how to generate a random number from list of number only once.

The 'only once' make your number not random, because random imply the possibility of repeat.
You are looking for 'shuffle', just like with playing cards.
Shuffle on computer:
make an array of size 52 and store each card in the array. Cards are at positions 0 to 51.
Draw a number between 0 and 51, swap that card with the 1 at position 0.
Draw a number between 1 and 51, swap that card with the 1 at position 1.
Draw a number between 2 and 51, swap that card with the 1 at position 2.
Draw a number between 3 and 51, swap that card with the 1 at position 3.
and so on until only last card remain.
Then you read the array in order as you need, you will not draw the same card 2 times until the end of array.
 
Share this answer
 
Comments
BillWoodruff 20-Oct-18 5:34am    
My vote of #3. I don't think you clearly understand what the OP is doing here.

"The 'only once' make your number not random, because random imply the possibility of repeat."

A sequence of numbers selected randomly without duplication does not make them "not random." Of course, depending on the quantity of numbers generated, the set of numbers may exhibit more or less "accidental order."
Patrice T 20-Oct-18 8:04am    
Hi Bill,
a 3 is perfectly ok to me.
I agree that I can be wrong with the question, but the OP didn't comment, and nobody else gave an answer, just your comment.
BillWoodruff 22-Oct-18 2:07am    
For me, a vote of #3 is not intended to be a down-vote, and I am glad you are comfortable with that.

I was going to post some possibly useful code, but, will only do so if the OP responds to my query.

cheers, Bill
Patrice T 22-Oct-18 5:01am    
Hi Bill,
a #3 is not a downvote for me either, it don't remove rep points.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900