本文整理汇总了C#中IRandom.NextDouble方法的典型用法代码示例。如果您正苦于以下问题:C# IRandom.NextDouble方法的具体用法?C# IRandom.NextDouble怎么用?C# IRandom.NextDouble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRandom
的用法示例。
在下文中一共展示了IRandom.NextDouble方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Apply
public static LinearLinkage Apply(IRandom random, LinearLinkage p1, LinearLinkage p2) {
var length = p1.Length;
var child = new LinearLinkage(length);
var endNodes = new HashSet<int>();
for (var i = 0; i < length; i++) {
if ((p1[i] == i && p2[i] == i)
|| ((p1[i] == i || p2[i] == i) && random.NextDouble() < 0.5)) {
child[i] = i;
endNodes.Add(i);
}
}
for (var i = 0; i < length; i++) {
if (endNodes.Contains(i)) continue;
var p1End = endNodes.Contains(p1[i]);
var p2End = endNodes.Contains(p2[i]);
if ((p1End && p2End) || (!p1End && !p2End)) {
child[i] = random.NextDouble() < 0.5 ? p1[i] : p2[i];
} else if (p1End) {
child[i] = p1[i];
} else {
child[i] = p2[i];
}
}
child.LinearizeTreeStructures();
return child;
}
示例2: Apply
/// <summary>
/// Performs a breeder genetic algorithm manipulation on the given <paramref name="vector"/>.
/// </summary>
/// <param name="random">A random number generator.</param>
/// <param name="vector">The real vector to manipulate.</param>
/// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
/// <param name="searchIntervalFactor">The factor determining the size of the search interval.</param>
public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, DoubleValue searchIntervalFactor) {
int length = vector.Length;
double prob, value;
do {
value = Sigma(random);
} while (value == 0);
prob = 1.0 / (double)length;
bool wasMutated = false;
for (int i = 0; i < length; i++) {
if (random.NextDouble() < prob) {
double range = bounds[i % bounds.Rows, 1] - bounds[i % bounds.Rows, 0];
if (random.NextDouble() < 0.5) {
vector[i] = vector[i] + value * searchIntervalFactor.Value * range;
} else {
vector[i] = vector[i] - value * searchIntervalFactor.Value * range;
}
wasMutated = true;
}
}
// make sure at least one gene was mutated
if (!wasMutated) {
int pos = random.Next(length);
double range = bounds[pos % bounds.Rows, 1] - bounds[pos % bounds.Rows, 0];
if (random.NextDouble() < 0.5) {
vector[pos] = vector[pos] + value * searchIntervalFactor.Value * range;
} else {
vector[pos] = vector[pos] - value * searchIntervalFactor.Value * range;
}
}
}
示例3: GenerateRandomPointOnSurface
/// <summary>Returns a random point on the surface of a cylinder</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="orientation">Orientation of the cylinder</param>
/// <param name="radius">Radius of the cylinder</param>
/// <param name="length">Length of the cylinder</param>
/// <returns>A random point on the volume's surface</returns>
public static Vector3 GenerateRandomPointOnSurface(
IRandom randomNumberGenerator,
Matrix orientation, float radius, float length
) {
// Calculate the surface areas of the three sections our cylinder has:
// Upper cap, side and lower cap
float capArea = MathHelper.Pi * (radius * radius);
float sideArea = 2.0f * MathHelper.Pi * radius * length;
float capAndSideArea = capArea + sideArea;
// We need a phi value (angle of the random point) in any of the cases
float phi = (float)randomNumberGenerator.NextDouble() * MathHelper.TwoPi;
// Choose the section that the random point will be generated on in relation
// to its surface area so the probability is constant on the entire surface
float section = (float)randomNumberGenerator.NextDouble() * (capArea * 2.0f + sideArea);
// Depending on the section, these two values are calculated differently
float randomRadius;
float randomZ;
// Upper cap: Generate a random radius
if(section < capArea) {
randomZ = length / 2.0f;
randomRadius = (float)Math.Sqrt(randomNumberGenerator.NextDouble()) * radius;
// Side: Generate a random height
} else if(section < capAndSideArea) {
randomZ = ((float)randomNumberGenerator.NextDouble() - 0.5f) * length;
randomRadius = radius;
// Lower cap: Generate a random radius
} else {
randomZ = -length / 2.0f;
randomRadius = (float)Math.Sqrt(randomNumberGenerator.NextDouble()) * radius;
}
// Now transform the point to cartesian coordinates and rotate it into
// the global coordinate frame
return Vector3.Transform(
new Vector3(
randomRadius * (float)Math.Cos(phi),
randomRadius * (float)Math.Sin(phi),
randomZ
),
orientation
);
}
示例4: Randomize
private DoubleArray Randomize(IRandom random, int length, DoubleMatrix bounds) {
var result = new DoubleArray(length);
for (int i = 0; i < length; i++) {
result[i] = random.NextDouble() * bounds[i % bounds.Rows, 1] - bounds[i % bounds.Rows, 0];
}
return result;
}
示例5: Apply
/// <summary>
/// Performs the some positions bitflip mutation on a binary vector.
/// </summary>
/// <param name="random">The random number generator to use.</param>
/// <param name="vector">The vector that should be manipulated.</param>
/// <param name="pm">The probability a bit is flipped.</param>
public static void Apply(IRandom random, BinaryVector vector, DoubleValue pm) {
for (int i = 0; i < vector.Length; i++) {
if (random.NextDouble() < pm.Value) {
vector[i] = !vector[i];
}
}
}
示例6: SelectRandomTourBiasedByLength
protected static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance) {
int tourIndex = -1;
double sum = 0.0;
double[] probabilities = new double[individual.Tours.Count];
for (int i = 0; i < individual.Tours.Count; i++) {
probabilities[i] = 1.0 / ((double)individual.Tours[i].Stops.Count / (double)instance.Cities.Value);
sum += probabilities[i];
}
for (int i = 0; i < probabilities.Length; i++)
probabilities[i] = probabilities[i] / sum;
double rand = random.NextDouble();
double cumulatedProbabilities = 0.0;
int index = 0;
while (tourIndex == -1 && index < probabilities.Length) {
if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
tourIndex = index;
cumulatedProbabilities += probabilities[index];
index++;
}
return tourIndex;
}
示例7: ShakeLocalParameters
public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
// mutation
var d = random.NextDouble() * 2.0 - 1.0;
value += shakingFactor * d;
if (value < 0.1) value = 0.1;
if (value > 3) value = 3;
}
示例8: GenerateRandomPointWithin
/// <summary>Returns a random point within a disc</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the disc</param>
/// <returns>A random point within the disc</returns>
public static Vector2 GenerateRandomPointWithin(
IRandom randomNumberGenerator, float radius
) {
// Choose a random angle for the point
float phi = (float)randomNumberGenerator.NextDouble() * MathHelper.Pi * 2.0f;
// Get a random radius with compensated probability for the outer regions of the disc
float r = (float)Math.Sqrt(randomNumberGenerator.NextDouble()) * radius;
// Calculate the final position of the point in world space
return new Vector2(
r * (float)Math.Cos(phi),
r * (float)Math.Sin(phi));
}
示例9: Randomize
public virtual void Randomize(IRandom random, int startIndex, int length, double min, double max) {
double delta = max - min;
if (length > 0) {
for (int i = 0; i < length; i++)
array[startIndex + i] = min + delta * random.NextDouble();
OnReset();
}
}
示例10: GenerateRandomPointOnSurface
/// <summary>Returns a random point on the surface of a box</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="extents">Extents of the box</param>
/// <returns>A random point on the box' surface</returns>
/// <remarks>
/// The performance of this algorithm varies slightly depending on the face
/// that is chosen for the random point because a different number of
/// comparisons and subtractions will be performed.
/// </remarks>
public static Vector3 GenerateRandomPointOnSurface(
IRandom randomNumberGenerator, Vector3 extents
) {
// For this task, we also need the dimensions of the box
Vector3 dimensions = extents * 2.0f;
// Determine the area covered by the sides of the box
float leftRightArea = dimensions.Z * dimensions.Y;
float topBottomArea = dimensions.X * dimensions.Z;
float frontBackArea = dimensions.X * dimensions.Y;
// Choose which face of the box the point is going be on
float side = (float)randomNumberGenerator.NextDouble() *
(leftRightArea * 2.0f) * (topBottomArea * 2.0f) * (frontBackArea * 2.0f);
// Now obtain were the point will be located
float u = (float)randomNumberGenerator.NextDouble() * 2.0f - 1.0f;
float v = (float)randomNumberGenerator.NextDouble() * 2.0f - 1.0f;
// Calculate the final world space coordinates of the point
side -= leftRightArea;
if(side < 0.0)
return new Vector3(-extents.X, v * extents.Y, u * extents.Z);
side -= leftRightArea;
if(side < 0.0)
return new Vector3(+extents.X, v * extents.Y, u * extents.Z);
side -= topBottomArea;
if(side < 0.0)
return new Vector3(u * extents.X, +extents.Y, v * extents.Z);
side -= topBottomArea;
if(side < 0.0)
return new Vector3(u * extents.X, -extents.Y, v * extents.Z);
side -= frontBackArea;
if(side < 0.0)
return new Vector3(u * extents.X, v * extents.Y, -extents.Z);
else
return new Vector3(u * extents.X, v * extents.Y, +extents.Z);
}
示例11: ShakeLocalParameters
public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
base.ShakeLocalParameters(random, shakingFactor);
// 50% additive & 50% multiplicative
if (random.NextDouble() < 0.5) {
double x = NormalDistributedRandom.NextDouble(random, Symbol.ManipulatorMu, Symbol.ManipulatorSigma);
Value = Value + x * shakingFactor;
} else {
double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeManipulatorSigma);
Value = Value * x;
}
}
示例12: Apply
/// <summary>
/// Performs a random convex crossover on the two given parents.
/// </summary>
/// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
/// <param name="random">The random number generator.</param>
/// <param name="parent1">The first parent vector for the crossover.</param>
/// <param name="parent2">The second parent vector for the crossover.</param>
/// <returns>The newly created real vector, resulting from the random convex crossover.</returns>
public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2) {
if (parent1.Length != parent2.Length)
throw new ArgumentException("ERROR in RandomConvexCrossover: the two parents are not of the same length");
int length = parent1.Length;
double[] result = new double[length];
double factor = random.NextDouble();
for (int i = 0; i < length; i++)
result[i] = (factor * parent1[i]) + ((1 - factor) * parent2[i]);
return new RealVector(result);
}
示例13: GenerateRandomPointWithin
/// <summary>Returns a random point within a sphere</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the sphere</param>
/// <returns>A random point with the sphere</returns>
public static Vector3 GenerateRandomPointWithin(
IRandom randomNumberGenerator, float radius
) {
// TODO: This is just an approximation. Find the real algorithm.
float r = (float)randomNumberGenerator.NextDouble();
r = (float)Math.Sqrt(r * r);
r = (float)Math.Sqrt(r * r);
return GenerateRandomPointOnSurface(randomNumberGenerator, r);
}
示例14: Apply
/// <summary>
/// Perfomrs a heuristic crossover on the two given parents.
/// </summary>
/// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
/// <param name="random">The random number generator.</param>
/// <param name="betterParent">The first parent for the crossover operation.</param>
/// <param name="worseParent">The second parent for the crossover operation.</param>
/// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
public static RealVector Apply(IRandom random, RealVector betterParent, RealVector worseParent) {
if (betterParent.Length != worseParent.Length)
throw new ArgumentException("HeuristicCrossover: the two parents are not of the same length");
int length = betterParent.Length;
double[] result = new double[length];
double factor = random.NextDouble();
for (int i = 0; i < length; i++) {
result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
}
return new RealVector(result);
}
示例15: GenerateRandomPointOnSurface
/// <summary>Returns a random point on the surface of a sphere</summary>
/// <param name="randomNumberGenerator">Random number generator that will be used</param>
/// <param name="radius">Radius of the sphere</param>
/// <returns>A random point on the sphere's surface</returns>
public static Vector3 GenerateRandomPointOnSurface(
IRandom randomNumberGenerator, float radius
) {
// Choose a random longitude for the point
float phi = (float)randomNumberGenerator.NextDouble() * MathHelper.TwoPi;
// The z axis goes straight through the sphere and is chosen in random.
float z = (float)randomNumberGenerator.NextDouble() * 2.0f - 1.0f;
// From that, we'll calculate the latitude this point will have on the
// sphere's surface.
float theta = (float)Math.Sqrt(1.0f - z * z);
// Calculate the final position of the point in world space
return new Vector3(
radius * theta * (float)Math.Cos(phi),
radius * theta * (float)Math.Sin(phi),
radius * z
);
}