Microsoft.Automata.Chooser.Choose C# (CSharp) Méthode

Choose() public méthode

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.
Résultat int
        public int Choose(int n)
        {
            if (n < 0)
            {
                throw new ArgumentOutOfRangeException("n", "n must be non-negative.");
            }

            if (n <= 1)
            {
                return 0;
            }

            uint result;
            uint msb = this.GetMostSignificantBit((uint)n);
            uint mask = msb - 1;

            double probabilityOfMsbBeingOne = ((double)n - msb) / n;

            if (this.ChooseDouble() < probabilityOfMsbBeingOne)
            {
                result = msb + (uint)this.Choose(n - (int)msb);
            }
            else
            {
                byte[] randomData = new byte[sizeof(uint)];
                Chooser.randomNumberGenerator.GetBytes(randomData);
                uint randomInt = BitConverter.ToUInt32(randomData, 0);

                result = randomInt & mask;
            }

            return (int)result;
        }

Usage Example

Exemple #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());
        }
All Usage Examples Of Microsoft.Automata.Chooser::Choose