File size: 3,778 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System.Collections.Generic;

namespace RandomOps
{
    /// <summary>
    /// Random Number Generator (RNG) that downloads random bytes from the internet
    /// website: www.random.org. Not thread-safe.
    /// These random bytes are generated from atmospheric noise picked up by radios.
    /// Only a certain number of bits are allowed to be downloaded from this website
    /// to your IP address each day, so this RNG is used with a Fallback-PRNG.
    /// </summary>
    public class RandomDotOrg : ByteStream
    {
        #region Constructor.
        /// <summary>
        /// Constructs the RNG-object.
        /// </summary>
        /// <param name="bufferSize">Number of random bytes the buffer holds.</param>
        /// <param name="randFallback">Fallback RNG to be used when buffer is empty.</param>
        /// <param name="numFallback">Use fallback RNG for this many bytes before trying to fill buffer again.</param>
        public RandomDotOrg(int bufferSize, Random randFallback, int numFallback)

            : base(bufferSize, randFallback, numFallback)
        {
        }
        #endregion

        #region Download random bytes from internet.
        /// <summary>
        /// Do the actual retrieval of random bytes from the www.random.org website.
        /// </summary>
        /// <param name="length">Number of bytes to retrieve (max 10000).</param>
        /// <returns>List of random bytes.</returns>
        /// <exception cref="WebException">
        /// Thrown if daily download quota is depleted,
        /// the HTTP response code is 503 for 'Server Unavailable'.
        /// </exception>
        public static List<byte> RetrieveBytes(int length)
        {
            List<byte> bytes = new List<byte>(length);

            string url = @"http://www.random.org/integers/?num=" + length + @"&min=0&max=255&col=1&base=10&format=plain&rnd=new";
            string data = HtmlDownload.HtmlToString(url);

            string[] dataArray = data.Split('\n');

            foreach (string s in dataArray)
            {
                if (s != null && s != "")
                {
                    byte b;

                    if (System.Byte.TryParse(s, out b))
                    {
                        bytes.Add(b);
                    }
                }
            }

            return bytes;
        }
        #endregion

        #region Base-class overrides.
        /// <summary>
        /// Name of the RNG.
        /// </summary>
        public override string Name
        {
            get { return "Random.Org Sync"; }
        }

        /// <summary>
        /// The www.random.org website only allows 10000 elements
        /// to be retrieved at once.
        /// </summary>
        protected override int MaxRetrieveLength
        {
            get { return 10000; }
        }

        /// <summary>
        /// Called from ByteStream to request the retrieval of random bytes.
        /// </summary>
        /// <param name="length">The number of bytes requested.</param>
        protected sealed override void DoFillBuffer(int length)
        {
            // Retrieve the bytes.
            IEnumerable<byte> bytes = RetrieveBytes(length);

            // Add the bytes to the ByteStream-buffer.
            base.AddBuffer(bytes);
        }
        #endregion
    }
}