    return To index

    C# Random Subroutine Sequencing
    -------------------------------
    alcopaul/brigada ocho
    june 06, 2011

    In HLL programs, instructions run from top to bottom, unless the programmer purposely uses goto commands.

    In C#, there's a way to make your program execute its instruction randomly. This is very hard to implement
    in other languages, me thinks.

    I thought that this is one way to change the static behavior of a program, which is usual in HLL.

    Anyway here's the source code. Just make a subroutine and add that to meths array.

    ----------------------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] meths = { "HH1", "HH2", "HH3", "HH4", "HH5", "HH6", "HH7", "HH8" };
                Random rand = new Random();
                for (int i = meths.Length - 1; i > 0; i--)
                {
                    int n = rand.Next(i + 1);
                    string temp = meths[i];
                    meths[i] = meths[n];
                    meths[n] = temp;
                }
                Assembly HostAsm = Assembly.GetExecutingAssembly();
                Type type = HostAsm.GetType("ConsoleApplication1.Program");
                object obj = Activator.CreateInstance(type);
                foreach (string j in meths)
                {
                    type.InvokeMember(j,
                                      BindingFlags.Default | BindingFlags.InvokeMethod,
                                      null,
                                      obj,
                                      null);
                }
                Console.ReadKey();
            }
            public static void HH1()
            {
                Console.WriteLine("1st");
            }
            public static void HH2()
            {
                Console.WriteLine("2nd");
            }
            public static void HH3()
            {
                Console.WriteLine("3rd");
            }
            public static void HH4()
            {
                Console.WriteLine("4th");
            }
            public static void HH5()
            {
                Console.WriteLine("5th");
            }
            public static void HH6()
            {
                Console.WriteLine("6th");
            }
            public static void HH7()
            {
                Console.WriteLine("7th");
            }
            public static void HH8()
            {
                Console.WriteLine("8th");
            }
        }
    }

    -------------------------------------------------------------------------------------