Choose() public method

Initializes a new instance of the Chooser class. Gets or sets the random seed of the chooser. The value of this property is ignored. The property is needed for backward compatibility. Returns a nonnegative random number less than the specified maximum.
public Choose ( int n ) : int
n int The exclusive upper bound of the random number to be generated. n must be /// greater than or equal to zero.
return int
Esempio n. 1
0
        /// <summary>
        /// Generates a random member accepted by fa.
        /// Assumes that fa has no dead states, or else termination is not guaranteed.
        /// </summary>
        public string GenerateMember(Automaton <HashSet <Tuple <char, char> > > fa)
        {
            if (fa.IsEmpty)
            {
                throw new AutomataException(AutomataExceptionKind.AutomatonMustBeNonempty);
            }
            var sb    = new System.Text.StringBuilder();
            int state = fa.InitialState;

            while (!fa.IsFinalState(state) || (fa.OutDegree(state) > 0 && chooser.ChooseTrueOrFalse()))
            {
                var move = fa.GetNthMoveFrom(state, chooser.Choose(fa.GetMovesCountFrom(state)));
                if (!move.IsEpsilon)
                {
                    Tuple <char, char> someRange = new Tuple <char, char>('\0', '\0');
                    foreach (var range in move.Label)
                    {
                        someRange = range;
                        break;
                    }
                    int  offset   = chooser.Choose(Math.Max(1, (int)someRange.Item2 - (int)someRange.Item1));
                    char someChar = (char)((int)someRange.Item1 + offset);
                    sb.Append(someChar);
                }
                state = move.TargetState;
            }
            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Produces a random path of labels from the initial state to some final state.
        /// Assumes that the automaton is nonempty and does not contain deadends.
        /// </summary>
        /// <param name="chooser">uses the chooser for randomizing the choices</param>
        public IEnumerable <S> ChoosePathToSomeFinalState(Chooser chooser)
        {
            if (IsEmpty)
            {
                throw new AutomataException(AutomataExceptionKind.AutomatonMustBeNonempty);
            }

            int state = InitialState;

            while (!IsFinalState(state) || (OutDegree(state) > 0 && chooser.ChooseTrueOrFalse()))
            {
                if (!IsFinalState(state) && OutDegree(state) == 0)
                {
                    throw new AutomataException(AutomataExceptionKind.AutomatonMustNotContainDeadStates);
                }

                var move = GetNthMoveFrom(state, chooser.Choose(GetMovesCountFrom(state)));
                if (!move.IsEpsilon)
                {
                    yield return(move.Label);
                }

                state = move.TargetState;
            }
            yield break;
        }
Esempio n. 3
0
        /// <summary>
        /// Generates a random member accepted by fa.
        /// Assumes that fa has no dead states, or else termination is not guaranteed.
        /// </summary>
        public string GenerateMember(Automaton <HashSet <char> > fa)
        {
            if (fa.IsEmpty)
            {
                throw new AutomataException(AutomataExceptionKind.AutomatonMustBeNonempty);
            }
            var sb    = new System.Text.StringBuilder();
            int state = fa.InitialState;

            while (!fa.IsFinalState(state) || (fa.OutDegree(state) > 0 && chooser.ChooseTrueOrFalse()))
            {
                var move = fa.GetNthMoveFrom(state, chooser.Choose(fa.GetMovesCountFrom(state)));
                if (!move.IsEpsilon)
                {
                    char someChar = '\0';
                    foreach (var c in move.Label)
                    {
                        someChar = c;
                        break;
                    }
                    sb.Append(someChar);
                }
                state = move.TargetState;
            }
            return(sb.ToString());
        }
Esempio n. 4
0
        /// <summary>
        /// Choose a random member uniformly at random from the ulong set.
        /// </summary>
        public char ChooseUniformly(ulong s)
        {
            int K = (int)ComputeDomainSize(s);
            int k = chooser.Choose(K);

            for (int i = 0; i < atoms.Length; i++)
            {
                if (IsSatisfiable(atoms[i] & s))
                {
                    if (k < partition[i].Count)
                    {
                        return((char)partition[i][k]);
                    }
                    else
                    {
                        k = k - partition[i].Count;
                    }
                }
            }
            throw new AutomataException(AutomataExceptionKind.SetIsEmpty);
        }