Skip to content

Commit

Permalink
Added CatSAT.Random.SetSeed()
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhorswill committed Aug 29, 2018
1 parent 75badf0 commit ba91f9a
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions CatSAT/SAT/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ internal static class Random
{
#if XORSHIFT
private static uint state = 234923840;

/// <summary>
/// Set the seed to a specified value.
/// </summary>
public static void SetSeed(uint seed)
{
state = seed;
}

/// <summary>
/// Set the seed to the current time (System.DateTime.Now.Ticks)
/// </summary>
public static void SetSeed()
{
SetSeed((uint)System.DateTime.Now.Ticks);
}

public static uint Next()
{
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
Expand All @@ -59,10 +76,10 @@ public static uint Next()
}
#endif

/// <summary>
/// Return a random integer in [0, max)
/// </summary>
public static uint InRange(uint max)
/// <summary>
/// Return a random integer in [0, max)
/// </summary>
public static uint InRange(uint max)
{
return Next() % max;
}
Expand All @@ -89,7 +106,7 @@ public static uint InRange(uint min, uint max)
/// </summary>
public static T RandomElement<T>(this List<T> list)
{
return list[(int)InRange((uint)list.Count)];
return list[(int) InRange((uint) list.Count)];
}

/// <summary>
Expand Down Expand Up @@ -374,7 +391,7 @@ public static uint Prime()
public static float Float(float min, float max)
{
double unitInterval = Next() / ((double) uint.MaxValue);
return min + (max - min) * (float)unitInterval;
return min + (max - min) * (float) unitInterval;
}
}
}

0 comments on commit ba91f9a

Please sign in to comment.