private static readonly Random oti = new Random( Guid.NewGuid().ToByteArray().Sum( b => b * b.GetHashCode() ) ); private static readonly ReaderWriterLockSlim otia = new ReaderWriterLockSlim( LockRecursionPolicy.SupportsRecursion ); public static T RandEnum() { if ( !typeof( T ).IsEnum ) { return default( T ); } var names = Enum.GetNames( typeof( T ) ); try { otia.EnterWriteLock(); return ( T )Enum.Parse( typeof( T ), names[ oti.Next( 0, names.Length ) ] ); } finally { otia.ExitWriteLock(); } }
Sunday, October 31, 2010
Random Enum in C#
I wrote this because.. well.. I wanted to see how well it would work.. 8D
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment