Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:22:44+00:00 2026-05-26T22:22:44+00:00

Ok, suppose I have a class foo with a state propriety. I need a

  • 0

Ok, suppose I have a class foo with a “state” propriety. I need a function which chooses randomly a new state with these restrictions: Some transitions are not allowed and every transition has a different probability to happen.

enum PossibleStates { A, B, C, D }
PossibleStates currentState;

float[,] probabilities;

probabilities[s1, s2] tells the probability of going from state s1 to state s2. It can also be 0.0f meaning that s1->s2 isn’t possible and probabilities[s1, s2] can be different from probabilities[s2, s1]. I need this method to be as general as possible, so that there can be three as well as three-hundred possible states.
This isn’t homework, I just need a good starting point because I don’t know where to begin from 🙂

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:22:45+00:00Added an answer on May 26, 2026 at 10:22 pm

    fully functional example following. F5 this then tap ENTER to change state.
    in the States class you can define your probabilities and states.

    in my code i assume routes are mutually exclusive(their combined probability never goes above 1). i’ve marked the piece of code to change if that’s not true.

    namespace RandomStatesProgram
    {
        class State
        {
            public string Name;
    
            private bool current;
            public bool Current
            {
                get { return current; }
                set
                {
                    if (current)
                    {
                        if (value) StayingHere();
                        else LeavingState();
                    }
                    else
                    {
                        if (value) EnteringState();
                    }
    
                    current = value;
                }
            }
    
            public void StayingHere() { Console.WriteLine("Staying in state " + this.Name); }
            public void EnteringState() { Console.WriteLine("Entering state " + this.Name); }
            public void LeavingState() { Console.WriteLine("Leaving state " + this.Name); }
    
            public State()
            {
                this.Name = "New";
                this.Current = false;
            }
    
            public State(string name)
                : this()
            {
                this.Name = name;
            }
        }
    
        class TransitionCourse
        {
            public State From { get; set; }
            public State To { get; set; }
            public float Probability { get; set; }
    
            public TransitionCourse(Dictionary<int, State> allStates, int fromState, int toState, float probability)
            {
                if (probability < 0 || probability > 1)
                    throw new ArgumentOutOfRangeException("Invalid probability");
    
                if (!allStates.Keys.Any(K => K == fromState || K == toState))
                    throw new ArgumentException("State not found");
    
                this.From = allStates[fromState];
                this.To = allStates[toState];
                this.Probability = probability;
            }
        }
    
        static class States
        {
            private static Dictionary<int, State> PossibleStates;
            public static State Current
            {
                get
                {
                    if (PossibleStates.Where(S => S.Value.Current).Count() == 1)
                        return PossibleStates.Single(S => S.Value.Current).Value;
                    else
                        return null;
                }
            }
    
            public static List<TransitionCourse> Transitions;
    
            static States()
            {
                PossibleStates = new Dictionary<int, State>()
                {
                    {1, new State("One")}, 
                    {2, new State("Two")}, 
                    {3, new State("Three")},
                    {4, new State("Four")} 
                };
    
                // example: 50% chance of switching to either state from every one of the three
                // note: it must be  0 <= 3rd param <= 1 of course (it's a probability)
                Transitions = new List<TransitionCourse>()
                {
                    new TransitionCourse(PossibleStates,1,2,1f/3f),
                    new TransitionCourse(PossibleStates,1,3,1f/3f),
                    new TransitionCourse(PossibleStates,1,4,1f/3f),
                    new TransitionCourse(PossibleStates,2,1,1f),            
                    new TransitionCourse(PossibleStates,3,1,1f),
                    new TransitionCourse(PossibleStates,4,1,1f)               
                };
            }
    
            public static void GoTo(int targetState)
            {
                if (!PossibleStates.Keys.Contains(targetState))
                    throw new ArgumentException("Invalid state");
    
                foreach (KeyValuePair<int, State> state in PossibleStates.OrderByDescending(S=>S.Value.Current))
                {
                    //first is the "true" state (the current one) then the others.
                    //this way we go OUT from a state before going IN another one.
                    state.Value.Current = state.Key.Equals(targetState);
                }
            }
    
            public static void Travel()
            {
                if (Current == null)
                    throw new InvalidOperationException("Current state not set");
    
                TransitionCourse[] exits = Transitions.Where(T => T.From.Equals(Current)).OrderBy(T=>T.Probability).ToArray();
                if (exits.Length == 0) //nowhere to go from here
                    return;
                else
                    if (exits.Length == 1) //not much to choose here
                    {
                        GoTo(PossibleStates.Single(S => S.Value.Equals(exits.First().To)).Key);
                    }
                    else //ok now we have a choice
                    {
                        //we need a "random" number
                        double p = new Random().NextDouble();
    
                        // remapping probabilities so we can choose "randomly"
                        // this works IF the sum of all transitions probability does not exceed 1.
                        // if it does, at best this'll act weird
                        for (int i = 1; i < exits.Length; i++)
                        {
                            exits[i].Probability += exits[i - 1].Probability;
                            if (exits[i].Probability > p)
                            {
                                GoTo(PossibleStates.Single(S => S.Value.Equals(exits[i].To)).Key);
                                return;
                            }
                        }
                    }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                States.GoTo(1);
                while (Console.ReadLine().ToUpper() != "Q")
                {
                    States.Travel();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class Foo , with a private variable bar_ containing some
Suppose I have some class which has a property actor_ of type Actor .
Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have this class: template</*some other parameters here */class toggle> class Foo {
Suppose I have the following class Foo, that supports a function of any arity
Suppose I have some extension methods but also need to extend the object's state.
Suppose I have these abstract classes Foo and Bar : class Foo; class Bar;
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have: class Foo { public String Bar { get; set; } }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.