-4

I'm making a program that generates descriptions of people, elves and other races etc.

It involves A LOT of arrays and lists that have 1 word from it picked and then put into a sentence. A sample array would look like:

public string[] hairfront = {
        "side swept bangs", "straight bangs", "choppy bangs", "wispy bangs",
        "parted straight bangs", "parted choppy bangs", "blunt single length bangs",
        "bangs shorter on one side than the other", "pixi cut bangs"}

Random class would then pick a number and return the corresponding string wherever I have RAE(array name)

Example: "The person has " + RAE(hairfront)

I would like to make RAE as a array type that will have a method that picks a random string to be put into the sentence.

The following pseudo code describes what I want my code to look like:

new RAE(hair)={ "blah", "brown", "long"}
Public string generate_description "the persons hair is" RAE(hair)
it will become "the persons hair is brown"
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 1
    Can you show the code you have so far for this randomizer ? And what is exactly the question ? – quantdev Jun 03 '14 at 21:11
  • Can't really tell what the question is. Sounds like its just a requirement. – BradleyDotNET Jun 03 '14 at 21:14
  • its on paper right now and so i mostly only have sudo code atm. basically i want to call an create an array that when called in a string will choose a random index. i have many arrays though and so instead of randomizing each array individually i want to randomize all arrays i use. – user3704725 Jun 03 '14 at 21:15
  • RAE will basically be an array paired with a randomizer. so i do not need to randomize each individual array – user3704725 Jun 03 '14 at 21:18
  • Your pseudo code could use some work :) but I cleaned up the question a lot. Please review and improve where you feel is beneficial! – BradleyDotNET Jun 03 '14 at 22:04

1 Answers1

1

As you say, you can just wrap it in a class:

public class RandomStringGenerator
{
   private string[] data;  //Data holder
   private Random rng = new Random(); //Class level so it seeds once

   public RandomStringGenerator(string[] startData)
   {
       data = startData;
   }

   public string GetRandomElement()
   {
       return data[rng.Next(0, data.Length)];
   }
}

You pass it the initialized array, then call GetNextElement each time you want something new:

RandomArray hairRandomizer = new RandomArray (new string[] { "side swept bangs", "straight bangs", "choppy bangs", "wispy bangs", "parted straight bangs", "parted choppy bangs", "blunt single length bangs", "bangs shorter on one side than the other", "pixi cut bangs"});
string myHair = hairRandomizer.GetNextElement();

As AlexeiLevenkov notes, creating a lot of these at the same time is a poor choice. If you do that, you need to create the Random object at a higher level and pass it in:

public class RandomStringGenerator
{
   private string[] data;  //Data holder
   private Random rng; //Not instantiated since we pass it in

   public RandomStringGenerator(string[] startData, Random rngToUse)
   {
       data = startData;
       rng = rngToUse;
   }
   ... //Same as before
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Thank you so much I'm glad i wasn't completely incoherent – user3704725 Jun 03 '14 at 21:27
  • @user3704725, Your writing could use a bit of work, it took more effort than it should have for me to parse out that this is what you wanted. Formatting code as code helps, and its a bit rambly. Having a succinct problem statement is very useful as well. That being said, I'm glad I could help! – BradleyDotNET Jun 03 '14 at 21:30
  • +1. Side notes: naming - it is not really "random array", but rather some sort of "random words generator", "next" is not really next as it can return the same but it indeed matches `Random` naming; `Random` - be careful to use instance per class - if you create multiple instances at the same time you will endup with completely synchronized "random" selection - check http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Alexei Levenkov Jun 03 '14 at 22:01
  • @AlexeiLevenkov, great points! I added an alternate version with a passed in Random object and changed the names. Thanks for the feedback. – BradleyDotNET Jun 03 '14 at 22:07