コード例 #1
0
ファイル: MT19937.cs プロジェクト: xyfc/Yggdrasil
        public void Next()
        {
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            var rnd = new MT19937(852456);

            var actual = new int[100];

            for (var i = 0; i < 100; ++i)
            {
                actual[i] = rnd.Next();
                Console.Write("{0}, ", actual[i]);
                if (i % 10 == 9)
                {
                    Console.WriteLine();
                }
            }

            Assert.Equal(new int[]
            {
                -1016690674, -1637870728, -692783469, -323248999, -1533665771, -1800427152, 1662001996, 2084386684, -757433287, 120032001,
                -1705096166, -1591729151, 1623952413, -785304155, 1108132845, 834940583, -1061792856, 1292875383, 1530193206, -958834311,
                -521422406, -1476584847, 1644928184, 1260721938, -759842211, -299687162, -1378585833, 439242734, -1387971947, -1811371473,
                -1985714671, -1068376625, -483596985, -1378429075, -2094662442, -1881951118, -2078293020, -1151429745, 2018416447, 1893879858,
                1326341598, -87012482, 327821473, -1207695435, 235559859, 2134760270, -1323840169, 1267399315, 531294005, -786136418,
                -453675209, 150317649, -1794148827, -1948085738, -1255531484, -1764054824, 767062180, 401275128, -1732525675, 1339059654,
                52391037, 1805419262, 1510642622, -1869031373, 1921352644, 1081859104, -1066381150, -1383572376, -251843853, 202259689,
                -1714270235, -1892268470, -1011817250, 1412463953, 220111516, -1565265572, 576040432, -420655050, -196418315, -193552307,
                -196005994, 2095968849, -1782680971, -1027966515, -468055061, -377912262, 439807374, 691554137, 520414605, 2002406186,
                719115332, 986992980, 620206509, 123073363, 1440013716, 557741451, -1815315156, -1496982324, -427519681, 380279830,
            }, actual);

            rnd.Init(456321);

            for (var i = 0; i < 100; ++i)
            {
                actual[i] = rnd.Next();
                Console.Write("{0}, ", actual[i]);
                if (i % 10 == 9)
                {
                    Console.WriteLine();
                }
            }

            Assert.Equal(new int[]
            {
                1823309890, 1286026978, 1700772777, -759084818, -1939449827, -1969269924, -1324637702, 1237751839, 1579467185, -1896356529,
                -1662531782, -1290070889, -1340729546, -462290590, 1893630519, 883728674, 817296192, -355085204, 1134931239, 1948959821,
                -564395120, -2099114907, 42843971, 478456745, 1761551733, -1914916423, 1044512791, 1455306705, -946573955, -728202319,
                1294804702, -1632067386, -186955106, 1032025171, -1373329726, 1691086447, 864646121, 1575177187, -1100242554, -874273313,
                1445571059, -1557846233, 626484120, -237187437, -888254806, -1897784757, -898232603, -78541345, -1069259125, -1336913358,
                -647024102, -940540444, 1392542934, -714307271, 523185203, -1169442940, -314834783, -124191978, 928627036, 180896026,
                -882290420, 28937894, -847412374, -597367692, 1166787309, 2034421376, 1224967835, 157701398, 1421075282, -2007011874,
                2009755335, 257512274, -111371144, 1646144901, 1911530226, -1144353162, 149005087, 917397968, -157892715, 917308110,
                -835347687, -1916905346, 2068358733, -83070125, 1597863186, -1886652522, 126063062, -1220114000, 81072986, 126356288,
                876814749, -745490213, 1334729031, 323609085, -1118853331, -1672600046, 89372839, -1890792525, 255461946, -195118879,
            }, actual);
        }
コード例 #2
0
        public static int CacheFile(byte[] data)
        {
            Monitor.Enter(_lock);
            int tmp = _rand.Next();

            while (_files.ContainsKey(tmp))
            {
                tmp = _rand.Next();
            }
            _files.Add(tmp, data);
            _cachedTimes.Add(tmp, DateTime.Now);
            Monitor.Exit(_lock);
            return(tmp);
        }
コード例 #3
0
        private static double r()
        {
            int    r   = (int)(rand.Next() % 16);
            double ret = (r - 8) * 0.25;

            //Console.WriteLine(ret);
            return(ret);
        }
コード例 #4
0
        private static void _ProcessEvent(object obj)
        {
            IEvent evnt = (IEvent)obj;

            lock (_rand){
                Thread.CurrentThread.Name = "EventProcessor{" + evnt.Name + "}[" + _rand.Next().ToString() + "]";
            }
            Thread.CurrentThread.IsBackground = true;
            IEventHandler[] handlers = new IEventHandler[0];
            Monitor.Enter(_lock);
            handlers = new IEventHandler[_handlers.Count];
            _handlers.CopyTo(handlers);
            Monitor.Exit(_lock);
            foreach (IEventHandler handler in handlers)
            {
                Log.Trace("Checking if " + handler.GetType().FullName + " handles event " + evnt.Name);
                if (handler.HandlesEvent(evnt))
                {
                    Thread delThread = null;
                    Log.Trace("Handling event " + evnt.Name + " with handler " + handler.GetType().FullName);
                    IAsyncResult res = _eventHandle.BeginInvoke(handler, evnt, ref delThread, null, null);
                    if (res.AsyncWaitHandle.WaitOne(_EVENT_HANDLING_TIMEOUT))
                    {
                        _eventHandle.EndInvoke(ref delThread, res);
                    }
                    else
                    {
                        Log.Error("Handle " + handler.GetType().FullName + " timed out handling event " + evnt.Name);
                        try
                        {
                            delThread.Abort();
                        }
                        catch (Exception e) { }
                        _eventHandle.EndInvoke(ref delThread, res);
                    }
                }
            }
        }