Exemplo n.º 1
0
        public (bool success, string possibleError) Init(object parameter, int seed)
        {
            if (parameter == null)
            {
                // If no parameters are given
                this.generateType        = typeof(int);
                this.chosenMode          = GenerateMode.Random;
                this.currentMinInclusive = IntegerGenerator.defaultMinIntInclusive;
                this.currentMaxExclusive = IntegerGenerator.defaultMaxIntExclusive;
            }
            else if (parameter.GetType() == typeof(string))
            {
                string tempParameter = (string)parameter;
                // Parse parameters
                foreach (string separator in rangeSeparators)
                {
                    if (tempParameter.Contains(separator))
                    {
                        string[] splitted = tempParameter.Split(new string[] { separator }, StringSplitOptions.None);
                        if (splitted.Length != 2)
                        {
                            return(success : false, possibleError : "Range for random should contain exactly 2 items, e.g. 13-4536");
                        }

                        if (long.TryParse(splitted[0], out long resultMin))
                        {
                            // Check if wanted min inclusive is int or long range
                            if (resultMin < IntegerGenerator.intMin || resultMin > IntegerGenerator.intMax)
                            {
                                this.currentMinInclusive = resultMin;
                            }
                            else
                            {
                                this.currentMinInclusive = (int)resultMin;
                            }
                        }
                        else
                        {
                            return(success : false, possibleError : $"{splitted[0]} is not number or it is outside of range {long.MinValue} .. {long.MaxValue}");
                        }

                        if (long.TryParse(splitted[1], out long resultMax))
                        {
                            // Check if wanted min inclusive is int or long range
                            if (resultMax < IntegerGenerator.intMin || resultMax > IntegerGenerator.intMax)
                            {
                                this.currentMaxExclusive = resultMax;
                            }
                            else
                            {
                                this.currentMaxExclusive = (int)resultMax;
                            }
                        }
                        else
                        {
                            return(success : false, possibleError : $"{splitted[0]} is not number or it is outside of range {long.MinValue} .. {long.MaxValue}");
                        }

                        // Check that min and max are same type
                        Type tOfMin = this.currentMinInclusive.GetType();
                        Type tOfMax = this.currentMaxExclusive.GetType();

                        if (tOfMin != tOfMax)
                        {
                            // If one is int and another is long, turn both to long
                            if (tOfMin == typeof(int))
                            {
                                this.currentMinInclusive = (long)(int)this.currentMinInclusive;
                            }

                            if (tOfMax == typeof(int))
                            {
                                this.currentMaxExclusive = (long)(int)this.currentMaxExclusive;
                            }
                        }

                        this.generateType = this.currentMinInclusive.GetType();
                        this.chosenMode   = GenerateMode.Random;
                    }
                }
            }
            else
            {
                return(success : false, possibleError : ErrorMessages.UnsupportedParameterType(LongName, "Init", parameter.GetType()));
            }

            if (this.chosenMode == GenerateMode.Random)
            {
                this.rng        = new Random(seed);
                this.storedSeed = seed;
                this.NextStep();
            }
            else if (this.chosenMode == GenerateMode.WeightedRandom)
            {
                // TODO: Add code
            }

            return(success : true, possibleError : "");
        }