locked
how to make a random pattern generator c# RRS feed

  • Question

  • I know how to make a random number generator, but I want to make a random pattern generator of letters?

    but only with this letters:  E G B D F F A C E C# F# G# Bb   .

    so like this: 

    E A  E A E A

    or 

    E G B  B G E  E G B

    or

    F A C E  F A C E 

    Friday, June 26, 2020 1:15 PM

All replies

  • Create a helper method that accepts the min/max length of the string (and optionally any sort of parameters that you might need such as the char classes to use, min class counts, etc).

    To get the length of the value generate a random number between the min and max. Create an array (or use a static one if the values are fixed) containing all the value characters you want to use. For each character in the output you want get a random index in the array. Store the corresponding character from the array into the output value.

    //NOT TESTED!!!
    static string GetRandomString ( int minLength, int maxLength )
    {
       //Get the length
       var len = s_rnd.Next(minLength, maxLength + 1);
    
       var builder = new stringBuilder();
       for (var index = 0; index < len; ++index)
       {
          var item = s_rnd.Next(0, s_charClasses.Length);
    
          //Might actually append a larger string than maxLength, is this an issue??
          builder.Append(s_charClasses[item]);
       };
    
       return builder.ToString();
    }
    
    //Assuming this doesn't change
    static string[] s_charClasses = new string[] { "E", G", "B" };
    static Random s_rnd = new Random();
    Note that because you're using strings instead of character classes it would be possible to generate a string larger than allowed. I didn't account for that here. If you need to then adjust the loop to run until the builder has more characters than desired and then truncate it. Alternatively (and more common) just use characters and not string parts.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 26, 2020 1:46 PM
  • are you using Static?
    Friday, June 26, 2020 1:58 PM
  • I did just for convenience. It doesn't matter as far as the impl goes.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 26, 2020 2:20 PM
  • ok I just r tried this:

       string GetRandomString(int minLength, int maxLength)
            {
                //Get the length
                var len = s_rnd.Next(minLength, maxLength + 1);
    
                var builder = new StringBuilder();
                for (var index = 0; index < len; ++index)
                {
                    var item = s_rnd.Next(0, s_charClasses.Length);
    
                    //Might actually append a larger string than maxLength, is this an issue??
                    builder.Append(s_charClasses[item]);
                };
    
                return builder.ToString();
            }
    
            //Assuming this doesn't change
            static string[] s_charClasses = new string[] { "E", "G", "B", "D", "F", "F", "A", "C", "E", "C#", "F#", "G#", "Bb"};      
            static Random s_rnd = new Random();
    
            private void button1_Click(object sender, EventArgs e)
            {
                Random maxLength = new Random();
                int rInt = maxLength.Next(4, 10);
                richTextBox1.Text = GetRandomString(4, rInt);
            }

    but it is not giving me  pattern. I am  getting this:

    E F D E F Bb

    E F# G# B F# E

    C A A F#

    did I do something wrong?

    Friday, June 26, 2020 2:47 PM
  • I don't understand. You wanted something to generate a random set of strings so that is what it does. Did I misunderstand your requirement? If so then please be very specific about the rules. Don't just provide examples as there is no way to understand rules from examples.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 26, 2020 2:51 PM
  • yeah I wanted it to be a in a pattern :

    like

    EA  EA  EA

    or 

    E G B   B G E   E G B  BGE

    or

    F A C E  F A C E 

    they are in a pattern that repeats

    Friday, June 26, 2020 3:12 PM
  • The examples don't explain the rules though. How many "groups" are you looking for? What separates the groups? Doe each group only consist of the strings you mentioned earlier? Are these valid?

    EB E G 

    C#F# C# F#

    Please provide the exact rules for building a pattern that you consider valid.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 26, 2020 3:43 PM
  • EB E G is not a pattern 

    CF# CF# is

    I only added the space to make it easier to see the Pattern.

    you know how in IQ tests  the give you a Pattern and tell you to finish it ?

    that is what I am looking for.

    so the save it random, as long as it is a Pattern.

    EE EE EE is a pattern

    FE FE FE is a pattern 

    So a pattern is something that repeats. like a sine wave  

     

    Friday, June 26, 2020 3:59 PM
  • This is going to be tough to implement because simple, repeating patterns are easy to pick off. Given your original example:

    E A  E A E A - Repeating pattern of E A

    or 

    E G B  B G E  E G B - Repeating pattern of E G B B GE but the second pattern would be incomplete. Repeating pattern of E G B then reversed? Still missing the rest of the second one.

    or

    F A C E  F A C E - repeating pattern of F A C E

    So I think you first need to identify the patterns you want (e.g. repeating, pattern/inverse, etc). The first step would be to generate the initial value. The function I posted earlier does that. Then you have a higher level method that produces the pattern from the original value.

    string RepeatPattern ( string pattern )
    {
       return pattern + pattern;
    }
    
    string InversePattern ( string pattern )
    {
       return pattern + ReverseString(pattern);
    }

    The problem with the reverse string though is that your initial values contained sets of strings and that will be hard to reverse correctly and might introduce ambiguity. Example

    allowed values: E F F#

    pattern: F F#

    Is the pattern a char followed by the char with a # or is F and F# two separate chars? You as the builder of the pattern would know but someone taking the test isn't going to know that F# is a valid token but # is not. So I think you need to rethink the original allowed values and limit them to simply characters. Then those characters can be combined in an arbitrary group. Then the group can be patterned using whatever rules you want (repeat, inverse, etc).

    So, in summary, create the base group you want using the original method provided. Then run that group through the pattern generator function(s) you want to support to produce a pattern string. If you want to add more patterns later you can expand on it by just adding a new method. Note that your code is going to have to know what pattern is being used so it can determine the "correct" answer as well so while the above assumes just a series of strings you're going to need to store more information to know the correct answer as well.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Naomi N Friday, June 26, 2020 4:47 PM
    Friday, June 26, 2020 4:31 PM
  • You are making this way harder than it has to be.  All you need is something that generate a random sequence, like Michael did, for example "E F F# E".  Once you have that, you can convert it into a repeating sequence by, well, repeating it.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, June 29, 2020 5:15 AM