Skip to content

Instantly share code, notes, and snippets.

@DartPower
Last active October 23, 2024 09:15
Show Gist options
  • Save DartPower/a5e1f0bfe5b901067665d6c6955ad522 to your computer and use it in GitHub Desktop.
Save DartPower/a5e1f0bfe5b901067665d6c6955ad522 to your computer and use it in GitHub Desktop.
mcs -langversion:6 "MersenneTwister.cs" // Steam "A Valley Without Wind" KeyGen
using System;
internal class MersenneTwister
{
private const float BaseFloatDivisor = 10000f;
private const long LOWER_MASK = 2147483647L;
private const int M = 397;
private const long MATRIX_A = 2567483615L;
private const int N = 624;
private const long TEMPERING_MASK_B = 2636928640L;
private const long TEMPERING_MASK_C = 4022730752L;
private const long UPPER_MASK = 2147483648L;
public int GenerationCount;
public int LastSeed;
private static long[] mag01;
private readonly long[] mt;
private short mti;
static MersenneTwister()
{
mag01 = new long[2] { 0L, 2567483615L };
}
public MersenneTwister(int seed)
{
mt = new long[624];
LastSeed = 0;
GenerationCount = 0;
ReinitializeWithSeed(seed);
}
protected long GenerateUInt()
{
long num2;
if (mti >= 624)
{
short num;
for (num = 0; num < 227; num++)
{
num2 = (mt[num] & 0x80000000u) | (mt[num + 1] & 0x7FFFFFFF);
mt[num] = mt[num + 397] ^ (num2 >> 1) ^ mag01[(int)(IntPtr)(num2 & 1)];
}
while (num < 623)
{
num2 = (mt[num] & 0x80000000u) | (mt[num + 1] & 0x7FFFFFFF);
mt[num] = mt[num + -227] ^ (num2 >> 1) ^ mag01[(int)(IntPtr)(num2 & 1)];
num++;
}
num2 = (mt[623] & 0x80000000u) | (mt[0] & 0x7FFFFFFF);
mt[623] = mt[396] ^ (num2 >> 1) ^ mag01[(int)(IntPtr)(num2 & 1)];
mti = 0;
}
short num3 = mti++;
num2 = mt[num3];
num2 ^= TEMPERING_SHIFT_U(num2);
num2 ^= TEMPERING_SHIFT_S(num2) & 0x9D2C5680u;
num2 ^= TEMPERING_SHIFT_T(num2) & 0xEFC60000u;
num2 ^= TEMPERING_SHIFT_L(num2);
GenerationCount++;
return num2;
}
public int Next()
{
return Next(int.MaxValue);
}
public int Next(int maxValue)
{
if (maxValue <= 1)
{
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException();
}
return 0;
}
return (int)(GenerateUInt() % maxValue);
}
public int Next(int minValue, int maxValue)
{
if (maxValue <= minValue)
{
return minValue;
}
return Next(maxValue - minValue) + minValue;
}
public bool NextBool()
{
return Next(0, 2) == 1;
}
public double NextDouble(double MaxValue)
{
if (MaxValue < 0.0)
{
throw new ArgumentOutOfRangeException();
}
return (double)((float)GenerateUInt() / 10000f) % MaxValue;
}
public double NextDouble(double MinValue, double MaxValue)
{
if (MaxValue <= MinValue)
{
return MinValue;
}
return NextDouble(MaxValue - MinValue) + MinValue;
}
public float NextFloat(float MaxValue)
{
if (MaxValue < 0f)
{
throw new ArgumentOutOfRangeException();
}
return (float)GenerateUInt() / 10000f % MaxValue;
}
public float NextFloat(float MinValue, float MaxValue)
{
if (MaxValue <= MinValue)
{
return MinValue;
}
return NextFloat(MaxValue - MinValue) + MinValue;
}
public int NextInclus(int minValue, int maxValue)
{
return Next(minValue, maxValue + 1);
}
public int NextWithInclusiveUpperBound(int minValue, int maxValue)
{
return Next(minValue, maxValue + 1);
}
public void ReinitializeWithSeed(int seed)
{
if (seed == 0)
{
seed = 1;
}
else if (seed < 0)
{
seed = -seed;
}
LastSeed = seed;
mt[0] = seed & 0xFFFFFFFFu;
for (mti = 1; mti < 624; mti++)
{
mt[mti] = (69069 * mt[mti - 1]) & 0xFFFFFFFFu;
}
}
private static long TEMPERING_SHIFT_L(long y)
{
return y >> 18;
}
private static long TEMPERING_SHIFT_S(long y)
{
return y << 7;
}
private static long TEMPERING_SHIFT_T(long y)
{
return y << 15;
}
private static long TEMPERING_SHIFT_U(long y)
{
return y >> 11;
}
[STAThread]
public static void Main(string[] args)
{
try
{
int num = 1;
const int MAX_VALUE = 99999;
Console.WriteLine("Generating...");
System.IO.FileStream fs = new System.IO.FileStream(".\\output.log", global::System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
while (num <= MAX_VALUE)
{
var mersenneTwister = new MersenneTwister(num + 7);
var text = $"{num}-";
for (int i = 0; i < 3; i++)
{
if (i > 0)
{
text += "-";
}
text += mersenneTwister.Next(10, 99);
}
mersenneTwister = null;
Console.Title = $"Progress: [{new string('.', (int)(num * 100 / MAX_VALUE))}] {(int)(num * 100 / MAX_VALUE)}%";
sw.WriteLine(text);
num++;
}
sw.Close();
fs.Close();
Console.WriteLine("Generated keys saved to file output.log");
Console.ReadLine();
}
catch (Exception ex)
{
try
{
Console.WriteLine($"[Error]: {ex.Message}");
}
catch
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment