diff --git a/Syllabore/Syllabore/NameGenerator.cs b/Syllabore/Syllabore/NameGenerator.cs index 7712b72..d3cc81d 100644 --- a/Syllabore/Syllabore/NameGenerator.cs +++ b/Syllabore/Syllabore/NameGenerator.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json; namespace Syllabore { @@ -17,7 +13,7 @@ namespace Syllabore /// individual syllables of the name. /// /// - public class NameGenerator : INameGenerator + public class NameGenerator : INameGenerator, IRandomizable { /// @@ -33,7 +29,10 @@ public class NameGenerator : INameGenerator /// private const int DefaultMaximumRetries = 1000; - private Random _random; + /// + /// Used to simulate randomness during name generation. + /// + public Random Random { get; set; } /// /// @@ -137,7 +136,7 @@ public NameGenerator(ISyllableGenerator provider, INameTransformer transformer, this.UsingFilter(filter); } - _random = new Random(); + this.Random = new Random(); } #endregion @@ -419,7 +418,7 @@ public string Next() throw new InvalidOperationException("The value of property MinimumSyllables must be less than or equal to property MaximumSyllables. Both values must be postive integers."); } - var syllableLength = _random.Next(this.MinimumSyllables, this.MaximumSyllables + 1); + var syllableLength = this.Random.Next(this.MinimumSyllables, this.MaximumSyllables + 1); return this.Next(syllableLength); } @@ -450,7 +449,7 @@ public Name NextName() throw new InvalidOperationException("The value of property MinimumSyllables must be less than or equal to property MaximumSyllables. Both values must be postive integers."); } - var syllableLength = _random.Next(this.MinimumSyllables, this.MaximumSyllables + 1); + var syllableLength = this.Random.Next(this.MinimumSyllables, this.MaximumSyllables + 1); return this.NextName(syllableLength); } @@ -490,7 +489,7 @@ public Name NextName(int syllableLength) } } - if (this.Transformer != null && _random.NextDouble() < this.TransformerChance) + if (this.Transformer != null && this.Random.NextDouble() < this.TransformerChance) { result = this.Transformer.Apply(result); } diff --git a/Syllabore/Syllabore/SyllableGenerator.cs b/Syllabore/Syllabore/SyllableGenerator.cs index 5197d86..54434ce 100644 --- a/Syllabore/Syllabore/SyllableGenerator.cs +++ b/Syllabore/Syllabore/SyllableGenerator.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.Serialization; using System.Text; -using System.Text.Json.Serialization; namespace Syllabore { @@ -102,7 +100,7 @@ public enum SyllablePosition /// a . /// [Serializable] - public class SyllableGenerator : ISyllableGenerator + public class SyllableGenerator : ISyllableGenerator, IRandomizable { // These probabilities are applied if custom values are not specified private const double DefaultChanceLeadingConsonantExists = 0.95; // Only applies if consonants are supplied @@ -115,10 +113,14 @@ public class SyllableGenerator : ISyllableGenerator private const double DefaultChanceFinalConsonantBecomesSequence = 0.25; // Only if consonant final sequences are supplied - private Random _random { get; set; } private Context _context { get; set; } // For contextual command .Sequences() private List _lastChanges { get; set; } // For contextual command .Weight() + /// + /// Used to simulate randomness during syllable generation. + /// + public Random Random { get; set; } + /// /// Leading consonants are consonants that appear before /// the vowel within a syllable. @@ -302,7 +304,7 @@ public bool LeadingVowelSequenceForStartingSyllableAllowed /// public SyllableGenerator() { - _random = new Random(); + this.Random = new Random(); this.Probability = new GeneratorProbability(); _context = Context.None; _lastChanges = new List(); @@ -623,7 +625,7 @@ private string NextLeadingConsonant() if(this.LeadingConsonants.Count > 0) { //return this.LeadingConsonants[this.Random.Next(this.LeadingConsonants.Count)].Value; - return this.LeadingConsonants.RandomWeightedItem().Value; + return this.LeadingConsonants.RandomWeightedItem(this.Random).Value; } else { @@ -636,7 +638,7 @@ private string NextLeadingConsonantSequence() if (this.LeadingConsonantSequences.Count > 0) { //return this.LeadingConsonantSequences[this.Random.Next(this.LeadingConsonantSequences.Count)].Value; - return this.LeadingConsonantSequences.RandomWeightedItem().Value; + return this.LeadingConsonantSequences.RandomWeightedItem(this.Random).Value; } else { @@ -649,7 +651,7 @@ private string NextVowel() if (this.Vowels.Count > 0) { //return this.Vowels[this.Random.Next(this.Vowels.Count)].Value; - return this.Vowels.RandomWeightedItem().Value; + return this.Vowels.RandomWeightedItem(this.Random).Value; } else { @@ -662,7 +664,7 @@ private string NextVowelSequence() if (this.VowelSequences.Count > 0) { // return this.VowelSequences[this.Random.Next(this.VowelSequences.Count)].Value; - return this.VowelSequences.RandomWeightedItem().Value; + return this.VowelSequences.RandomWeightedItem(this.Random).Value; } else { @@ -674,7 +676,7 @@ private string NextTrailingConsonant() { if (this.TrailingConsonants.Count > 0) { - return this.TrailingConsonants.RandomWeightedItem().Value; + return this.TrailingConsonants.RandomWeightedItem(this.Random).Value; } else { @@ -686,7 +688,7 @@ private string NextTrailingConsonantSequence() { if (this.TrailingConsonantSequences.Count > 0) { - return this.TrailingConsonantSequences.RandomWeightedItem().Value; + return this.TrailingConsonantSequences.RandomWeightedItem(this.Random).Value; } else { @@ -699,7 +701,7 @@ private string NextFinalConsonant() { if (this.FinalConsonants.Count > 0) { - return this.FinalConsonants.RandomWeightedItem().Value; + return this.FinalConsonants.RandomWeightedItem(this.Random).Value; } else { @@ -711,7 +713,7 @@ private string NextFinalConsonantSequence() { if (this.FinalConsonantSequences.Count > 0) { - return this.FinalConsonantSequences.RandomWeightedItem().Value; + return this.FinalConsonantSequences.RandomWeightedItem(this.Random).Value; } else { @@ -751,10 +753,10 @@ private string GenerateSyllable(SyllablePosition position) if (position == SyllablePosition.Starting && this.LeadingVowelForStartingSyllableAllowed - && _random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelExists) + && this.Random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelExists) { - if (this.VowelSequencesAllowed && _random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelIsSequence) + if (this.VowelSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelIsSequence) { output.Append(this.NextVowelSequence()); } @@ -766,11 +768,11 @@ private string GenerateSyllable(SyllablePosition position) else { // Determine if there is a leading consonant in this syllable - if (this.LeadingConsonantsAllowed && _random.NextDouble() < this.Probability.ChanceLeadingConsonantExists) + if (this.LeadingConsonantsAllowed && this.Random.NextDouble() < this.Probability.ChanceLeadingConsonantExists) { // If there is a leading consonant, determine if it is a sequence - if (this.LeadingConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceLeadingConsonantIsSequence) + if (this.LeadingConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceLeadingConsonantIsSequence) { output.Append(this.NextLeadingConsonantSequence()); } @@ -781,11 +783,11 @@ private string GenerateSyllable(SyllablePosition position) } // Determine if there is a vowel in this syllable (by default, this probability is 100%) - if(this.VowelsAllowed && _random.NextDouble() < this.Probability.ChanceVowelExists) + if(this.VowelsAllowed && this.Random.NextDouble() < this.Probability.ChanceVowelExists) { // Then check if the vowel is a vowel sequence - if (this.VowelSequencesAllowed && _random.NextDouble() < this.Probability.ChanceVowelIsSequence) + if (this.VowelSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceVowelIsSequence) { output.Append(this.NextVowelSequence()); } @@ -801,11 +803,11 @@ private string GenerateSyllable(SyllablePosition position) // (as opposed to a 'trailing' consonant) if(position == SyllablePosition.Ending && this.FinalConsonantsAllowed - && _random.NextDouble() < this.Probability.ChanceFinalConsonantExists) + && this.Random.NextDouble() < this.Probability.ChanceFinalConsonantExists) { // We need to append a final consonant, // but we need to determine if the consonant is a sequence first - if (this.FinalConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceFinalConsonantIsSequence) + if (this.FinalConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceFinalConsonantIsSequence) { output.Append(this.NextFinalConsonantSequence()); } @@ -815,11 +817,11 @@ private string GenerateSyllable(SyllablePosition position) } } // Otherwise, roll for a trailing consonant - else if (this.TrailingConsonantsAllowed && _random.NextDouble() < this.Probability.ChanceTrailingConsonantExists) + else if (this.TrailingConsonantsAllowed && this.Random.NextDouble() < this.Probability.ChanceTrailingConsonantExists) { // If we need to append a trailing consonant, check if // we need a sequence instead - if (this.TrailingConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceTrailingConsonantIsSequence) + if (this.TrailingConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceTrailingConsonantIsSequence) { output.Append(this.NextTrailingConsonantSequence()); } diff --git a/Syllabore/Syllabore/SyllableSet.cs b/Syllabore/Syllabore/SyllableSet.cs index 98a85bb..e1e2a7b 100644 --- a/Syllabore/Syllabore/SyllableSet.cs +++ b/Syllabore/Syllabore/SyllableSet.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; namespace Syllabore { @@ -18,8 +16,14 @@ namespace Syllabore /// culture, historical period, etc. /// /// - public class SyllableSet : ISyllableGenerator + public class SyllableSet : ISyllableGenerator, IRandomizable { + + /// + /// Used to simulate randomness during generation. + /// + public Random Random { get; set; } + /// /// The syllable set size for starting syllables. /// @@ -72,6 +76,7 @@ public SyllableSet() : this(8, 8, 8) /// public SyllableSet(int startingSyllableCount, int middleSyllableCount, int endingSyllableCount) { + this.Random = new Random(); this.StartingSyllables = new HashSet(); this.MiddleSyllables = new HashSet(); this.EndingSyllables = new HashSet(); @@ -188,7 +193,7 @@ public string NextStartingSyllable() } } - return this.StartingSyllables.RandomItem(); + return this.StartingSyllables.RandomItem(this.Random); } /// @@ -205,7 +210,7 @@ public string NextSyllable() } } - return this.MiddleSyllables.RandomItem(); + return this.MiddleSyllables.RandomItem(this.Random); } /// @@ -221,7 +226,7 @@ public string NextEndingSyllable() } } - return this.EndingSyllables.RandomItem(); + return this.EndingSyllables.RandomItem(this.Random); } } diff --git a/Syllabore/Syllabore/TransformSet.cs b/Syllabore/Syllabore/TransformSet.cs index 0709aa6..d4fdd17 100644 --- a/Syllabore/Syllabore/TransformSet.cs +++ b/Syllabore/Syllabore/TransformSet.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; namespace Syllabore @@ -22,9 +20,12 @@ namespace Syllabore /// /// [Serializable] - public class TransformSet : INameTransformer + public class TransformSet : INameTransformer, IRandomizable { - private Random _random; + /// + /// Used to simulate randomness when is true. + /// + public Random Random { get; set; } /// /// The Transforms that make up this @@ -56,7 +57,7 @@ public class TransformSet : INameTransformer /// public TransformSet() { - _random = new Random(); + this.Random = new Random(); this.Transforms = new List(); this.UseRandomSelection = false; this.RandomSelectionCount = 0; @@ -91,7 +92,7 @@ private Name ApplyRandomTransforms(Name sourceName) for (int i = 0; i < this.RandomSelectionCount && unusedTransforms.Count > 0; i++) { - var transform = unusedTransforms.RandomWeightedItem(); + var transform = unusedTransforms.RandomWeightedItem(this.Random); unusedTransforms.Remove(transform); if (this.CanTransformBeApplied(transform, sourceName))