File size: 2,630 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 |
/// ------------------------------------------------------
/// 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;
namespace RandomOps
{
/// <summary>
/// Summing the output of multiple RNGs and taking modulo 2^32.
/// Note that this assumes the RNGs have RandMax roughly equal
/// to UInt32.MaxValue otherwise there will be a bias. Thread-safe
/// if RNGs are thread-safe.
/// </summary>
/// <remarks>
/// If you are using RNGs that have custom methods for generating
/// random numbers then you need to extend this class in a fashion
/// similar to that of the Uniform()-method.
/// </remarks>
public partial class SumUInt32 : RanUInt32
{
#region Constructor.
/// <summary>
/// Constructs the RNG-object from different RNG's.
/// </summary>
public SumUInt32(RanUInt32[] rands)
: base()
{
Rands = rands;
}
#endregion
#region Internal variables.
/// <summary>
/// The array of RNGs to sum.
/// </summary>
protected Random[] Rands
{
get;
private set;
}
#endregion
#region Base-class overrides.
/// <summary>
/// Name of the RNG.
/// </summary>
public override sealed string Name
{
get
{
string s = "Sum(";
foreach (Random rand in Rands)
{
s += rand.Name + ", ";
}
s += ")";
return s;
}
}
/// <summary>
/// Draw a random number in inclusive range {0, .., RandMax}
/// </summary>
public override sealed UInt32 Rand()
{
UInt32 sum = 0;
// Sum and modulo.
foreach (RanUInt32 rand in Rands)
{
sum += rand.Rand();
}
return sum;
}
/// <summary>
/// The maximum possible value returned by Rand().
/// </summary>
public override sealed UInt32 RandMax
{
get
{
return UInt32.MaxValue;
}
}
#endregion
}
}
|