summaryrefslogtreecommitdiff
path: root/dsa/DSALib/Auxiliary/RandomMisc.cs
blob: 27239306903a90ee51bb002f5d151845a5c15a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Linq;
using System.Text;

namespace DSALib.Auxiliary
{
    public static class RandomMisc
    {
        private static readonly Random Rand = new Random();

        // use: 4w6 +4
        public static string Roll(string input)
        {
            var output = new StringBuilder();
            var strings = input.Split('w', 'd').ToList();
            var count = Convert.ToInt32(strings[0]);
            strings = strings[1].Split(' ').ToList();
            var d = Convert.ToInt32(strings[0]);

            if (strings.Count > 0)
            {
            }

            var sum = 0;
            for (var i = 0; i < count; i++)
            {
                var roll = Dice.Roll(d);
                sum += roll;
                output.Append("[" + roll + "] ");
            }

            if (strings.Count > 1)
            {
                sum += Convert.ToInt32(strings[1]);
                output.Append("sum: " + sum);
            }

            return output.ToString();
        }

        public static double Random(double stdDev = 1, double mean = 0)
        {
            var u1 = Rand.NextDouble(); // uniform(0,1) random doubles
            var u2 = Rand.NextDouble();
            var randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                Math.Sin(2.0 * Math.PI * u2); // random normal(0,1)
            var randNormal =
                mean + stdDev * randStdNormal; // random normal(mean,stdDev^2)
            return randNormal;
        }
    }
}