0

I am working on a game in which characters are randomly generated, including their names. I intend for the names to be broken into 2-4 parts (prefix, middle, suffix); each part will be a short (1-4 character) string that will be randomly selected from a file and combined to create the full name. For example, if the selected parts from the file were 'bor', 'o', and 'mir', the character's name should read "Boromir".

What would be the ideal way to populate this file (like I said it will need to include lots of 1-4 character strings), serialize it, and then read in a random selection of those strings as needed?

I don't really have many ideas to go off of; I've never done anything like this at all before, but any tips or advice would certainly be appreciated. Thanks.

Jason D
  • 2,634
  • 6
  • 33
  • 67
  • The file is just a sequence of unordered names? or is there a structure? i.e. first x names in the file can be used as 'first', second y names can be used in middle. – PeskyGnat Jun 03 '13 at 15:47
  • I thought about doing it both ways: the first idea being that each string would be fair game for any name part, and the other idea is that there would be designated strings for prefix, middle, and suffix. I figure the latter would probably be better. – Jason D Jun 03 '13 at 15:51
  • Not sure what you mean by "ideal" - that depends on the constraints of your problem. If you need some sort of ordering/indexing/searching capability then a file is not ideal. Consider a SQL Compact / SQL Express database or something like that. (I assume this is for a mobile app.) – Kev Jun 03 '13 at 15:54
  • could those "name" be in a database? if so you could make a primary integer autoincrement and query the database to know the biggest number and then make a random in that range – Rémi Jun 03 '13 at 15:56
  • I would use separate files or tables for the different parts. It doesn't make a lot of sense to put them in the same file, or table if they don't relate to each other as a unique "record". Would you be open to that? prefix.txt middle.txt and suffix.txt and then read each of those into an array and get random ones to generate your names. I can expound in an answer if you like. – BlakeH Jun 03 '13 at 16:04
  • I would certainly be open to that. Please propose an answer if you wouldn't mind. – Jason D Jun 03 '13 at 16:06

2 Answers2

1

Lets say you store the names in a text file with one name subpart per line, you can read all the names into a string[] with the method ReadAllLines() from the System.IO.File class

http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx

Similarly, you can write the string[] back out to a file using WriteAllLines()

http://msdn.microsoft.com/en-us/library/92e05ft3.aspx

After reading the file into a string[], you can select a random string from the array using the System.Random class Next() method

http://msdn.microsoft.com/en-us/library/9b3ta19y.aspx

Use the modulus operator to constrain the result to valid indices of the string[]

PeskyGnat
  • 2,454
  • 19
  • 22
  • You don't need to use the modulus operator. `Random.Next(100)` will give you a number from 0-99, inclusive. So if your string array is called `names`, then `Random.Next(names.Length)` will constrain the selection to the bounds of the array. And modulus, by the way, is a terrible way to do the constraint, because it can introduce skew. See http://stackoverflow.com/a/10984975/56778 – Jim Mischel Jun 03 '13 at 16:50
  • You're right, Next(int) is a better way, but it also suffers from a different type of bias..though I doubt any of this matters in the context of the original question – PeskyGnat Jun 03 '13 at 17:20
0

So I coded up a quick example of what I was hinting at in my comment. I used a windows forms application as my example run time. When the form loads, it reads the name parts from the individual files once, and then gets random values to form a name.

The name parts would be stored in plain text files with a part per line like so:

prefix.txt

far
bar
dun
brat

You would use the same format for each of the files (one word per line).

Now, the fun part:

public partial class Form1 : Form
    {
        private readonly Random _random = new Random();
        private string[] _prefixParts;
        private string[] _middleParts;
        private string[] _suffixParts;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Only read the data from each file once as this could be expensive
            _prefixParts = PopulateFromFile("prefix.txt"); // alter your path as necessary
            _middleParts = PopulateFromFile("middle.txt");
            _suffixParts = PopulateFromFile("suffix.txt");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Generate a random name
            randomNameLabel.Text = GetRandomName();
        }

        private string[] PopulateFromFile(string path)
        {
            return File.ReadAllLines(path);
        }

        private string GetRandomName()
        {
            // this will get a random name part from each of the arrays, concatenate and return the complete name
            return GetRandomNamePart(_prefixParts) + GetRandomNamePart(_middleParts) + GetRandomNamePart(_suffixParts);
        }

        private string GetRandomNamePart(string[] array)
        {
            // this will return a random value from the array
            return array[_random.Next(array.Length)];
        }
    }
BlakeH
  • 3,354
  • 2
  • 21
  • 31
  • Thanks for the reply. I'll have to check it out a bit later. My MVVM Light decided to stop working and now my program won't run at all. – Jason D Jun 03 '13 at 16:32
  • Sure thing, I'm around most days. So if you have questions let me know – BlakeH Jun 03 '13 at 16:33
  • Perhaps you could help me out with this. I have each of the text files in NameSpace.Utility.Names.nameXXX.txt - The class you posted is in NS.Utility.Names as well. How what would the paths need to be for me to access the text files? Every way that I have tried has caused the program to crash. I posted a question about it but thus far nobody has really been helpful. If you could help me out with this I'd really appreciate it. – Jason D Jun 03 '13 at 21:14
  • You can mark the text files with Build Action: Content, and set Copy to Output Directory: Copy Always. That should copy the files into the relevant bin folder and you won't need to supply a path, just the name of the file – BlakeH Jun 04 '13 at 01:38