Home > Software engineering >  C# - Generate Random String from 4 Textfiles
C# - Generate Random String from 4 Textfiles

Time:03-05

Hey i want to generate Random Texts from 4 different Textfiles. The first File have 2 Textslines, the secound have 2 Textline the thrid have 3 textlines and the fourth have 1 textline. With that it should be possible to generate 2x2x3x1 different texts.

I dont know why but the programm altimes just generate 2 different texts and then it runs in a endless loop.

Any idea what i can do to avoid this? - Normaly it should end with 4 different texts.

If i add to the other textfiles with just 2 lines 1 additional line, so that all files have 3 lines and just the last textfile have 1 line it works.

But normaly it should work with 2 lines too.

the Lists count 2 Lines and randam should generate random number between 0 and 2 minus 1 so its alltimes random between 0 and 1 and should select a random index in the list 0 or 1. But with 2 elements in the list it alltimes select the same index.

I use the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VKTextGenerator
{
    class Program
    {
        string sFullText = "";
        string sFirstTextPart = "";
        string sSecoundTextPart = "";
        string sThirdTextPart = "";
        string sLinkText = "";
        int iAnzahlTexte = 4;



        static void Main(string[] args)
        {
            Random rnd = new Random();
            Program self = new Program();

            List<string> allTexts = new List<string>();

            self.deleteFullTextFile();

            for (int i = 0; i < self.iAnzahlTexte; i  )
            {

                self.ReadFirstTextPart();
                Thread.Sleep(rnd.Next(10, 5000));
                self.ReadSecoundTextPart();
                Thread.Sleep(rnd.Next(10, 5000));
                self.ReadThirdTextPart();
                self.ReadLinkText();

                self.sFullText = self.sFirstTextPart   " "   self.sSecoundTextPart   " "   self.sThirdTextPart   " "   self.sLinkText;


                if (!allTexts.Contains(self.sFullText))
                {
                    allTexts.Add(self.sFullText);
                    self.AppendTextToFile();
                }
                else
                {
                    i--;
   
                }
            }
        }

        internal static string GetStringSha256Hash(string text)
        {
            if (String.IsNullOrEmpty(text))
                return String.Empty;

            using (var sha = new System.Security.Cryptography.SHA256Managed())
            {
                byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
                byte[] hash = sha.ComputeHash(textData);
                return BitConverter.ToString(hash).Replace("-", String.Empty);
            }
        }

        public void deleteFullTextFile()
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\FinishedText\";
            VerifyDir(path);
            string fileName = "FullTexts.txt";

            if (File.Exists(path   fileName))
            {
                File.Delete(path   fileName);
            }
        }

        public void ReadFirstTextPart()
        {
            List<string> textList = new List<string>();
            Random rnd = new Random();

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\Config\";
            string fileName = "FirstTextPart.txt";

            textList = ReadTextFile(path   fileName);
            if (textList.Count > 0)
            {
                sFirstTextPart = textList[rnd.Next(0, textList.Count - 1)];
            }
        }

        public void ReadSecoundTextPart()
        {
            List<string> textList = new List<string>();
            Random rnd = new Random();

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\Config\";
            string fileName = "SecoundTextPart.txt";

            textList = ReadTextFile(path   fileName);
            if (textList.Count > 0)
            {
                sSecoundTextPart = textList[rnd.Next(0, textList.Count - 1)];
            }
        }

        public void ReadThirdTextPart()
        {
            List<string> textList = new List<string>();
            Random rnd = new Random();

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\Config\";
            string fileName = "ThirdTextPart.txt";

            textList = ReadTextFile(path   fileName);
            if (textList.Count > 0)
            {
                sThirdTextPart = textList[rnd.Next(0, textList.Count - 1)];
            }
        }

        public void ReadLinkText()
        {
            List<string> textList = new List<string>();
            Random rnd = new Random();

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\Config\";
            string fileName = "LinkText.txt";

            textList = ReadTextFile(path   fileName);
            if (textList.Count > 0)
            {
                sLinkText = textList[rnd.Next(0, textList.Count - 1)];
            }
        }

        private static List<String> ReadTextFile(string path)
        {
            List<String> lines = new List<String>();
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
            {
                while (sr.Peek() != -1)
                    lines.Add(sr.ReadLine());
            }
            return lines;
        }

        public void AppendTextToFile()
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\FinishedText\";
            VerifyDir(path);
            string fileName = "FullTexts.txt";

            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(path   fileName, true);
                file.WriteLine(sFullText);
                file.Close();
            }
            catch (Exception) { }
        }

        public static String removeExeFromPathChar(String s)
        {
            int exeLength = "VKTextGenerator.exe".Length;

            return (s == null || s.Length == 0)
              ? null
              : (s.Substring(0, s.Length - exeLength));
        }

        public void VerifyDir(string path)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                if (!dir.Exists)
                {
                    dir.Create();
                }
            }
            catch { }
        }
    }
}

Here is a minified version of the problem. I realized now, that the problem is, if i have just 2 lines of text in my textfile where i want to select random index between 0 and 1. I got alltimes the same result for the random index of thy line rnd.Next(0, xxx.count -1) if the xxx.count are just 2 if its 3 or 4 its ok but with 2 it have problems and i dont understand why. the code is below.

Please help

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VKTextGenerator
{
    class Program
    {

        string sFullText = "";
        string sFirstTextPart = "";
        int iAnzahlTexte = 200;

        Random rnd = new Random();

        static void Main(string[] args)
        {

            Program self = new Program();

            List<string> allTexts = new List<string>();

            self.deleteFullTextFile();

            for (int i = 0; i < self.iAnzahlTexte; i  )
            {

                self.ReadFirstTextPart();
                Thread.Sleep(self.rnd.Next(10, 200));

                self.sFullText = self.sFirstTextPart   " "   self.sSecoundTextPart   " "   self.sThirdTextPart   " "   self.sLinkText;

                allTexts.Add(self.sFullText);
                self.AppendTextToFile();

            }
        }

        public static String removeExeFromPathChar(String s)
        {
            int exeLength = "VKTextGenerator.exe".Length;

            return (s == null || s.Length == 0)
                ? null
                : (s.Substring(0, s.Length - exeLength));
        }
        
        public void deleteFullTextFile()
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\FinishedText\";
            string fileName = "FullTexts.txt";

            if (File.Exists(path   fileName))
            {
                File.Delete(path   fileName);
            }
        }

        public void ReadFirstTextPart()
        {
            List<string> textList = new List<string>();

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\Config\";
            string fileName = "FirstTextPart.txt";

            textList = ReadTextFile(path   fileName);
            if (textList.Count > 0)
            {
                sFirstTextPart = textList[rnd.Next(0, textList.Count - 1)];
            }
        }

        private static List<String> ReadTextFile(string path)
        {
            List<String> lines = new List<String>();
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
            {
                while (sr.Peek() != -1)
                    lines.Add(sr.ReadLine());
            }
            return lines;
        }

        public void AppendTextToFile()
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = removeExeFromPathChar(path);
            path = path   @"\FinishedText\";
            string fileName = "FullTexts.txt";

            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(path   fileName, true);
                file.WriteLine(sFullText);
                file.Close();
            }
            catch (Exception) { }
        }

    }
}

CodePudding user response:

the answer is in the docs https://docs.microsoft.com/en-us/dotnet/api/system.random.next?view=net-6.0

32-bit signed integer that is greater than or equal to 0 and less than MaxValue.

rand.Next(0,1) will always generate 0. I have the same error in my code.

int lineNo = rand.Next(contents[i].Length - 1);

should be

 int lineNo = rand.Next(contents[i].Length);

BTW I wrote my code in order to show that yours is waay way too long

CodePudding user response:

here is a short and sweet one. I am not going to debug your original, you should be able to do it with a debugger

using System;
using System.Collections.Generic;
using System.Text;

class Program {

    public static void Main() {
        string[] files = new string[] { "one.txt" , "two.txt", "three.txt", "four.txt" };
        List<string[]> contents = new();
        foreach (string file in files) {
            var lines = File.ReadAllLines(file);
            contents.Add(lines);
        }
        var rand = new Random();
   
        for (int k = 0; k < 4; k  ) {
            var final = new StringBuilder();
            for (int i = 0; i < files.Length; i  ) {
                int lineNo = rand.Next(contents[i].Length - 1);
                final.AppendLine(contents[i][lineNo]);
            }
            Console.WriteLine(final.ToString());
        }

    }
}
  • Related