Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Random.cs « System « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03f8c5edcca8a8c71a4b076ebead6c347c827f48 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// System.Random.cs
//
// Author:
//   Bob Smith (bob@thestuff.net)
//
// (C) 2001 Bob Smith.  http://www.thestuff.net
//

using System;
using System.Globalization;

namespace System
{
		[Serializable]
        public class Random
        {
                private int S = 1;
                private const int A = 16807;
                private const int M = 2147483647;
                private const int Q = 127773;
                private const int R = 2836;

                public Random()
                {
                        S = (int)(DateTime.Now.Ticks);
                }

                public Random(int Seed)
                {
                        S = Seed;
                }

                public virtual int Next()
                {
                        return (int)(this.Sample()*Int32.MaxValue);
                }

                public virtual int Next(int maxValue)
                {
                        if (maxValue < 0)
                                throw new ArgumentOutOfRangeException(Locale.GetText (
					"Max value is less then min value."));
                        else if (maxValue == 0)
                                return 0;
                        return (int)(this.Sample()*maxValue);
                }

                public virtual int Next(int minValue, int maxValue)
                {
                        if (minValue > maxValue)
                                throw new ArgumentOutOfRangeException(Locale.GetText (
					"Min value is greater then max value."));
                        else if (minValue == maxValue)
                                return minValue;
                        return (int)(this.Sample()*(maxValue - minValue))+minValue;
                }
                public virtual void NextBytes(byte[] buffer)
                {
                        int i, l;
                        if (buffer == null)
                                throw new ArgumentNullException();
                        l = buffer.GetUpperBound(0);
                        for (i = buffer.GetLowerBound(0); i < l; i++)
                        {
                                buffer[i] = (byte)(this.Sample()*Byte.MaxValue);
                        }
                }

                public virtual double NextDouble ()
                {
                        return this.Sample();
                }

                protected virtual double Sample ()
		{
                        S = A*(S%Q)-R*(S/Q);
                        if (S < 0)
				S+=M;
                        return S/(double)Int32.MaxValue;
                }
        }
}