1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-06 02:11:06 +09:00
Files
lidgren-network-gen3/Lidgren.Network/NetRandomSeed.cs
lidgren 54ff6f1c37 Changed target framework to 4.5.1 for all projects
Changed some minor things after running .net portability analyzer
Fixed a bunch of warnings (mostly CLS compliance)
2014-08-10 09:27:36 +00:00

45 lines
1.1 KiB
C#

using System;
using System.Threading;
namespace Lidgren.Network
{
public static class NetRandomSeed
{
private static int m_seedIncrement = -1640531527;
/// <summary>
/// Generates a 32 bit random seed
/// </summary>
[CLSCompliant(false)]
public static uint GetUInt32()
{
ulong seed = GetUInt64();
uint low = (uint)seed;
uint high = (uint)(seed >> 32);
return low ^ high;
}
/// <summary>
/// Generates a 64 bit random seed
/// </summary>
[CLSCompliant(false)]
public static ulong GetUInt64()
{
#if !__ANDROID__ && !IOS && !UNITY_WEBPLAYER
ulong seed = (ulong)System.Diagnostics.Stopwatch.GetTimestamp();
seed ^= (ulong)Environment.WorkingSet;
ulong s2 = (ulong)Interlocked.Increment(ref m_seedIncrement);
s2 |= (((ulong)Guid.NewGuid().GetHashCode()) << 32);
seed ^= s2;
#else
ulong v1 = (ulong)Environment.TickCount;
v1 |= (((ulong)(new object().GetHashCode())) << 32);
ulong v2 = (ulong)Guid.NewGuid().GetHashCode();
v2 |= (((ulong)(Interlocked.Increment(ref m_seedIncrement)) << 32);
return v1 ^ v2;
#endif
return seed;
}
}
}