~angus.helm/terrella/trunk

« back to all changes in this revision

Viewing changes to Terrella/TerrellaRandom.cs

  • Committer: Angus Helm
  • Author(s):
  • Date: 2010-09-22 02:47:56 UTC
  • Revision ID: angus@angushelm.com-20100922024756-czuh9mmdt278n3y6
Converted to Ninject, started on battle map work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  along with Terrella.  If not, see <http://www.gnu.org/licenses/>.
18
18
 */
19
19
using System;
 
20
using Ninject;
20
21
using System.Collections.Generic;
21
22
 
22
23
namespace Terrella
28
29
        /// also provides some utility functions not found on standard
29
30
        /// Random classes.
30
31
        /// </summary>
31
 
        public static class TerrellaRandom
 
32
        public class TerrellaRandom
32
33
        {
33
 
                private static Random random;
34
 
                
35
 
                public static double NextRawDouble() {
36
 
                        return random.NextDouble();
 
34
                public Random RandomCore;
 
35
 
 
36
        [Inject]
 
37
        public TerrellaRandom()
 
38
        {
 
39
            this.RandomCore = new Random();
 
40
        }
 
41
 
 
42
                public double NextRawDouble() {
 
43
                        return RandomCore.NextDouble();
37
44
                }
38
 
                
39
 
                public static E Choice<E>(ICollection<E> source) {
 
45
 
 
46
        public E Choice<E>(IEnumerable<E> source)
 
47
        {
 
48
            return Choice(new List<E>(source));
 
49
        }
 
50
 
 
51
                public E Choice<E>(IList<E> source) {
40
52
                        int count = NextInt(0, source.Count - 1);
41
 
                        var en = source.GetEnumerator();
42
 
                        while (count > 0) {
43
 
                                en.MoveNext();
44
 
                                count--;
45
 
                        }
46
 
                        return en.Current;
47
 
                }
48
 
                
49
 
                public static double NextDouble(double min, double max) {
50
 
                        return ((max - min) * random.NextDouble()) + min;
51
 
                }
52
 
                
53
 
                public static int NextInt(int min, int max) {
54
 
                        return random.Next(min, max);
55
 
                }
56
 
                
57
 
                public static void SetRandom(Random r) {
58
 
                        random = r;
 
53
            return source[count];
 
54
                }
 
55
                
 
56
                public double NextDouble(double min, double max) {
 
57
                        return ((max - min) * RandomCore.NextDouble()) + min;
 
58
                }
 
59
                
 
60
                public int NextInt(int min, int max) {
 
61
                        return RandomCore.Next(min, max);
 
62
                }
 
63
                
 
64
                public void SetRandom(Random r) {
 
65
                        RandomCore = r;
59
66
                }
60
67
        }
61
68
}