コード例 #1
0
        /// <summary>
        /// Reads in two command-line arguments lo and hi and prints n uniformly
        /// random real numbers in [lo, hi) to standard output.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            // command-line arguments
            int n = int.Parse(args[0]);

            // for backward compatibility with Intro to Programming in Java version of RandomSeq
            if (args.Length == 1)
            {
                // generate and print n numbers between 0.0 and 1.0
                for (int i = 0; i < n; i++)
                {
                    double x = StdRandom.Uniform();
                    StdOut.Println(x);
                }
            }
            else if (args.Length == 3)
            {
                double lo = double.Parse(args[1]);
                double hi = double.Parse(args[2]);

                // generate and print n numbers between lo and hi
                for (int i = 0; i < n; i++)
                {
                    double x = StdRandom.Uniform(lo, hi);
                    StdOut.Printf("{0:f2}\n", x);
                }
            }
            else
            {
                throw new ArgumentException("Invalid number of arguments");
            }
        }
コード例 #2
0
ファイル: StdRandom.cs プロジェクト: lipengbest/NAlgs4
        /// <summary>
        /// Unit tests the methods in this class.
        /// </summary>
        /// <param name="args">the command-line arguments</param>
        private static void Test(string[] args)
        {
            int n = int.Parse(args[0]);

            if (args.Length == 2)
            {
                Seed = long.Parse(args[1]);
            }
            double[] probabilities = { 0.5, 0.3, 0.1, 0.1 };
            int[]    frequencies   = { 5, 3, 1, 1 };
            string[] a             = "A B C D E F G".Split(' ');

            StdOut.Println($"seed = {Seed}");
            for (int i = 0; i < n; i++)
            {
                StdOut.Printf("{0, 2}", Uniform(100));
                StdOut.Printf("{0,10:f5}", Uniform(10.0, 99.0));
                StdOut.Printf("{0, 7}", Bernoulli(0.5));
                StdOut.Printf("{0, 9:f5}", Gaussian(9.0, 0.2));
                StdOut.Printf("{0, 3}", Discrete(probabilities));
                StdOut.Printf("{0, 3}", Discrete(frequencies));
                StdOut.Printf("{0, 13}", Uniform(100000000000L));
                StdOut.Print("  ");
                Shuffle(a);
                foreach (string s in a)
                {
                    StdOut.Print(s);
                }
                StdOut.Println();
            }
        }