Skip to content

Instantly share code, notes, and snippets.

Created November 7, 2013 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7351029 to your computer and use it in GitHub Desktop.
Save anonymous/7351029 to your computer and use it in GitHub Desktop.
When built with Visual Studio 2012 for .NET 4.0, produces different results between all of .NET x86, .NET x64 and mono x86 on Windows. Tested on an i7-3820
using System;
using System.Runtime.InteropServices;
namespace randomtest
{
class Program
{
static void Main(string[] args)
{
randomtest.Random random = new randomtest.Random(0);
for (int i = 0; i < 10000; i++)
{
float valX = (float)random.NextDouble();
float valY = (float)random.NextDouble();
float normalizedX;
float normalizedY;
Normalize(valX, valY, out normalizedX, out normalizedY);
float result = DoCalc(normalizedX, normalizedY);
Console.WriteLine(result.ToString("G9", System.Globalization.CultureInfo.InvariantCulture));
}
}
static float DoCalc(float x, float y)
{
float direction = (float)Math.Atan2(y, x);
direction -= 1f / 30f;
float dirY = (float)Math.Sin(direction);
return dirY;
}
public static float DistanceSquared(float value1X, float value1Y)
{
return value1X * value1X + value1Y * value1Y;
}
public static void Normalize(float valueX, float valueY, out float resultX, out float resultY)
{
float factor = DistanceSquared(valueX, valueY);
if (factor == 0)
{
resultX = 0f;
resultY = 0f;
return;
}
factor = 1f / (float)Math.Sqrt(factor);
resultX = valueX * factor;
resultY = valueY * factor;
}
}
// Mono random
public class Random : System.Random
{
const int MBIG = int.MaxValue;
const int MSEED = 161803398;
int inext, inextp;
int[] SeedArray = new int[56];
public Random(int Seed)
{
int ii;
int mj, mk;
// Numerical Recipes in C online @ http://www.library.cornell.edu/nr/bookcpdf/c7-1.pdf
// Math.Abs throws on Int32.MinValue, so we need to work around that case.
// Fixes: 605797
if (Seed == System.Int32.MinValue)
mj = MSEED - System.Math.Abs(System.Int32.MinValue + 1);
else
mj = MSEED - System.Math.Abs(Seed);
SeedArray[55] = mj;
mk = 1;
for (int i = 1; i < 55; i++)
{ // [1, 55] is special (Knuth)
ii = (21 * i) % 55;
SeedArray[ii] = mk;
mk = mj - mk;
if (mk < 0)
mk += MBIG;
mj = SeedArray[ii];
}
for (int k = 1; k < 5; k++)
{
for (int i = 1; i < 56; i++)
{
SeedArray[i] -= SeedArray[1 + (i + 30) % 55];
if (SeedArray[i] < 0)
SeedArray[i] += MBIG;
}
}
inext = 0;
inextp = 31;
}
protected override double Sample()
{
int retVal;
if (++inext >= 56) inext = 1;
if (++inextp >= 56) inextp = 1;
retVal = SeedArray[inext] - SeedArray[inextp];
if (retVal < 0)
retVal += MBIG;
SeedArray[inext] = retVal;
return retVal * (1.0 / MBIG);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment