当前位置: 首页>>代码示例>>C#>>正文


C# Random.Shuffle方法代码示例

本文整理汇总了C#中System.Random.Shuffle方法的典型用法代码示例。如果您正苦于以下问题:C# Random.Shuffle方法的具体用法?C# Random.Shuffle怎么用?C# Random.Shuffle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Random的用法示例。


在下文中一共展示了Random.Shuffle方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FromHexString

        public void FromHexString()
        {
            Random random = new Random();

            string value;

            {
                byte[] buffer = new byte[32];
                random.NextBytes(buffer);

                value = NetworkConverter.ToHexString(buffer);
            }

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 1024 * 2; i++)
            {
                byte[] result1 = null;
                byte[] result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = NetworkConverter.FromHexString(value);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = NetworkConverter.FromHexString_2(value);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("FromHexString: " + sw1.Elapsed.ToString());
            sb.AppendLine("FromHexString_2: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:networkelements,项目名称:Library,代码行数:50,代码来源:Benchmark_FromHexString.cs

示例2: Copy

        public void Copy()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            var length = 1024 * 256;
            byte[] x1 = new byte[length];
            byte[] y1 = new byte[length];
            byte[] x2 = new byte[length];
            byte[] y2 = new byte[length];

            random.NextBytes(x1);
            random.NextBytes(x2);

            for (int i = 0; i < 1024 * 256; i++)
            {
                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        Unsafe.Copy(x1, 0, y1, 0, length);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        Array.Copy(x2, 0, y2, 0, length);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(x1, y1));
                Assert.IsTrue(Unsafe.Equals(x2, y2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Copy: " + sw1.Elapsed.ToString());
            sb.AppendLine("Array Copy: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:networkelements,项目名称:Library,代码行数:48,代码来源:Benchmark_Copy.cs

示例3: ToHexString

        public void ToHexString()
        {
            Random random = new Random();

            byte[] value = new byte[32];
            random.NextBytes(value);

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 1024 * 2; i++)
            {
                string result1 = null;
                string result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = NetworkConverter.ToHexString(value, 0, value.Length);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = NetworkConverter.ToHexString_2(value, 0, value.Length);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(result1 == result2);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("ToHexString: " + sw1.Elapsed.ToString());
            sb.AppendLine("ToHexString_2: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:networkelements,项目名称:Library,代码行数:44,代码来源:Benchmark_ToHexString.cs

示例4: Crc32_Castagnoli

        public void Crc32_Castagnoli()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            var length = 1024;
            byte[] value = new byte[length];

            for (int i = 0; i < 1024 * 1024; i++)
            {
                byte[] result1 = null;
                byte[] result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = Library.Security.Crc32_Castagnoli.ComputeHash(value, 0, length);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = T_Crc32_Castagnoli.ComputeHash(value, 0, length);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Unsafe Crc32_Castagnoli: " + sw1.Elapsed.ToString());
            sb.AppendLine("Managed Crc32_Castagnoli: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:44,代码来源:Benchmark_Crc32_Castagnoli.cs

示例5: Main

        public static void Main(string[] args)
        {
            System.Random TempRand = new System.Random();
            Configuration Config = new Configuration(args);
            using (var Bootstrapper = Utilities.IoC.Manager.Bootstrapper)
            {
                IEnumerable<IDataFormatter> Formatters = Bootstrapper.ResolveAll<IDataFormatter>();
                if (Formatters.Count() == 0)
                {
                    Console.WriteLine("No formatters found.");
                    return;
                }
                IEnumerable<ITimedTask> TimedTasks = TempRand.Shuffle(Bootstrapper.ResolveAll<ITimedTask>());
                if (TimedTasks.Count() == 0)
                {
                    Console.WriteLine("No tasks found.");
                    return;
                }

                using (var Profiler = Manager.StartProfiling())
                {
                    foreach (ITimedTask Task in TimedTasks)
                    {
                        for (int x = 0; x < Config.NumberIterations; ++x)
                        {
                            using (var Timer = Manager.Profile(Task.Name))
                            {
                                Task.Run();
                            }
                        }
                    }
                }
                var Result = Manager.StopProfiling(false);
                var Results = Result.Children
                                    .ForEach(x => new Result(x.Value.Entries, x.Key))
                                    .ToLookup(x => TimedTasks.FirstOrDefault(y => string.Equals(y.Name, x.Name, StringComparison.Ordinal))
                                                             .GetType()
                                                             .Attribute<SeriesAttribute>() as ISeries ?? new SeriesAttribute(""),
                                              x => x);
                Formatters.ForEach(x => x.Format(Results, Config.OutputDirectory));
            }
        }
开发者ID:modulexcite,项目名称:Sundial,代码行数:42,代码来源:Program.cs

示例6: NormalRoom

        public NormalRoom(NormalRoom prev, Random rand, bool noEvil)
        {
            var indexes = Enumerable.Range(0, roomTemplates.Length).ToList();
            rand.Shuffle(indexes);
            foreach (var index in indexes) {
                if (prev != null && index == prev.currentId)
                    continue;

                if ((roomTemplates[index].Flags & RoomFlags.Evil) != 0 && noEvil)
                    continue;

                if (prev != null) {
                    bool ok = false;
                    foreach (var conn in prev.ConnectionPoints) {
                        var d = conn.Item1.Reverse();
                        if (roomTemplates[index].Connections.Any(targetConn => targetConn.Item1 == d)) {
                            ok = true;
                            break;
                        }
                    }
                    if (!ok)
                        continue;
                }

                currentId = index;
            }
            current = roomTemplates[currentId];
        }
开发者ID:SirAnuse,项目名称:fabiano-swagger-of-doom,代码行数:28,代码来源:NormalRoom.cs

示例7: Equals

        public void Equals()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024; i++)
            {
                byte[] x;
                byte[] y;

                if (random.Next(0, 2) == 0)
                {
                    var length = random.Next(0, 1024 * 256);
                    x = new byte[length];
                    y = new byte[length];
                }
                else
                {
                    x = new byte[random.Next(0, 1024 * 256)];
                    y = new byte[random.Next(0, 1024 * 256)];
                }

                if (random.Next(0, 3) == 0)
                {
                    random.NextBytes(x);
                    random.NextBytes(y);
                }

                for (int j = 0; j < 32; j++)
                {
                    bool result1 = false;
                    bool result2 = false;

                    random.Shuffle(flags);
                    foreach (var index in flags)
                    {
                        if (index == 0)
                        {
                            sw1.Start();
                            result1 = Unsafe.Equals(x, y);
                            sw1.Stop();
                        }
                        else if (index == 1)
                        {
                            sw2.Start();
                            result2 = Benchmark.Equals(x, y);
                            sw2.Stop();
                        }
                    }

                    Assert.IsTrue(result1 == result2);
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Equals: " + sw1.Elapsed.ToString());
            sb.AppendLine("Unsafe Equals: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:networkelements,项目名称:Library,代码行数:65,代码来源:Benchmark_Equels.cs

示例8: WriteThreadsTest

            internal WriteThreadsTest(int num_values=10, int checkpoint_interval_rowcount=50, bool withMerge=false)
            {
                System.GC.Collect();
                db = new LayerManager(InitMode.NEW_REGION, "c:\\BENDtst\\11");
                // db.startMaintThread();
                this.checkpoint_interval = checkpoint_interval_rowcount;
                this.withMerge = withMerge;

                // init sequential data-values...
                datavalues = new int[num_values];
                for (int i = 0; i < num_values; i++) {
                    datavalues[i] = i;
                }

                // then shuffle them..
                Random rnd = new Random();
                rnd.Shuffle(datavalues);
            }
开发者ID:jeske,项目名称:StepsDB-alpha,代码行数:18,代码来源:LayerManagerTest_WriteThreads.cs

示例9: Shuffle

 public void Shuffle()
 {
     System.Random Rand = new System.Random(1231415);
     Assert.Equal(new int[] { 3, 1, 4, 5, 2 }, Rand.Shuffle(new int[] { 1, 2, 3, 4, 5 }));
 }
开发者ID:kaytie,项目名称:Craig-s-Utility-Library,代码行数:5,代码来源:RandomExtensions.cs

示例10: Xor

        public void Xor()
        {
            Random random = new Random();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 4; i++)
            {
                byte[] x;
                byte[] y;

                if (random.Next(0, 2) == 0)
                {
                    var length = random.Next(0, 1024 * 256);
                    x = new byte[length];
                    y = new byte[length];
                }
                else
                {
                    x = new byte[random.Next(0, 1024 * 256)];
                    y = new byte[random.Next(0, 1024 * 256)];
                }

                random.NextBytes(x);
                random.NextBytes(y);

                byte[] result1 = new byte[Math.Min(x.Length, y.Length)];
                byte[] result2 = new byte[Math.Min(x.Length, y.Length)];

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        Unsafe.Xor(x, y, result1);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        Benchmark.Xor(x, y, result2);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Unsafe Xor: " + sw1.Elapsed.ToString());
            sb.AppendLine("Managed Xor: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:59,代码来源:Benchmark_Xor.cs

示例11: Compare

        public void Compare()
        {
            Random random = new Random();

            Stopwatch totalStopwatch = new Stopwatch();
            totalStopwatch.Start();

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();
            Stopwatch sw3 = new Stopwatch();
            Stopwatch sw4 = new Stopwatch();

            var flags = new int[] { 0, 1, 2, 3 };

            for (int i = 0; i < 1024 * 256 * 32; i++)
            {
                byte[] x;
                byte[] y;

                if (random.Next(0, 2) == 0)
                {
                    var length = random.Next(0, 1024);
                    x = new byte[length];
                    y = new byte[length];
                }
                else
                {
                    x = new byte[random.Next(0, 1024)];
                    y = new byte[random.Next(0, 1024)];
                }

                if (random.Next(0, 2) == 0)
                {
                    random.NextBytes(x);
                    random.NextBytes(y);
                }

                int result1 = 0;
                int result2 = 0;
                int result3 = 0;
                int result4 = 0;

                var maxLength = Math.Min(x.Length, y.Length);

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = Unsafe.Compare(x, y);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = Unsafe.Compare2(x, y);
                        sw2.Stop();
                    }
                    else if (index == 2)
                    {
                        sw3.Start();
                        result3 = Unsafe.Compare(x, 0, y, 0, maxLength);
                        sw3.Stop();
                    }
                    else if (index == 3)
                    {
                        sw4.Start();
                        result4 = Unsafe.Compare2(x, 0, y, 0, maxLength);
                        sw4.Stop();
                    }
                }

                Assert.IsTrue(result1 == result2);
                Assert.IsTrue(result3 == result4);

                if (totalStopwatch.ElapsedMilliseconds > 1000 * 60) break;
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Native Compare: " + sw1.Elapsed.ToString());
            sb.AppendLine("Native Compare2: " + sw2.Elapsed.ToString());
            sb.AppendLine("Native Compare: " + sw3.Elapsed.ToString());
            sb.AppendLine("Native Compare2: " + sw4.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
开发者ID:networkelements,项目名称:Library,代码行数:88,代码来源:Benchmark_Compare.cs

示例12: DownloadManagerThread


//.........这里部分代码省略.........

                                if (items.Count > 0)
                                {
                                    round = (round >= items.Count) ? 0 : round;
                                    item = items[round++];
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }

                if (item == null) continue;

                try
                {
                    if (item.Rank == 1)
                    {
                        if (!_cacheManager.Contains(item.Seed.Key))
                        {
                            item.State = BackgroundDownloadState.Downloading;

                            _connectionsManager.Download(item.Seed.Key);
                        }
                        else
                        {
                            item.State = BackgroundDownloadState.Decoding;
                        }
                    }
                    else
                    {
                        if (!item.Index.Groups.All(n => _existManager.GetCount(n) >= n.InformationLength))
                        {
                            item.State = BackgroundDownloadState.Downloading;

                            int limitCount = 256;

                            foreach (var group in item.Index.Groups.ToArray().Randomize())
                            {
                                if (_existManager.GetCount(group) >= group.InformationLength) continue;

                                foreach (var key in _existManager.GetKeys(group, false))
                                {
                                    if (_connectionsManager.IsDownloadWaiting(key))
                                    {
                                        limitCount--;

                                        if (limitCount <= 0) goto End;
                                    }
                                }
                            }

                            List<Key> keyList = new List<Key>();

                            foreach (var group in item.Index.Groups.ToArray().Randomize())
                            {
                                if (_existManager.GetCount(group) >= group.InformationLength) continue;

                                List<Key> tempKeys = new List<Key>();

                                foreach (var key in _existManager.GetKeys(group, false))
                                {
                                    if (!_connectionsManager.IsDownloadWaiting(key))
                                    {
                                        tempKeys.Add(key);
                                    }
                                }

                                random.Shuffle(tempKeys);
                                foreach (var key in tempKeys)
                                {
                                    _connectionsManager.Download(key);

                                    limitCount--;
                                }

                                if (limitCount <= 0) goto End;
                            }

                        End: ;
                        }
                        else
                        {
                            item.State = BackgroundDownloadState.Decoding;
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = BackgroundDownloadState.Error;

                    Log.Error(e);

                    this.Remove(item);
                }
            }
        }
开发者ID:networkelements,项目名称:Library,代码行数:101,代码来源:BackgroundDownloadManager.cs

示例13: DownloadThread


//.........这里部分代码省略.........
                                    .Where(n => n.State == DownloadState.Downloading)
                                    .Where(n => n.Priority != 0)
                                    .OrderBy(n => -n.Priority)
                                    .ToList();

                                if (items.Count > 0)
                                {
                                    round = (round >= items.Count) ? 0 : round;
                                    item = items[round++];
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }

                if (item == null) continue;

                try
                {
                    if (item.Depth == 1)
                    {
                        if (!_cacheManager.Contains(item.Seed.Metadata.Key))
                        {
                            item.State = DownloadState.Downloading;

                            _connectionsManager.Download(item.Seed.Metadata.Key);
                        }
                        else
                        {
                            item.State = DownloadState.Decoding;
                        }
                    }
                    else
                    {
                        if (!item.Index.Groups.All(n => _existManager.GetCount(n) >= n.InformationLength))
                        {
                            item.State = DownloadState.Downloading;

                            var limitCount = (int)(256 * Math.Pow(item.Priority, 3));

                            foreach (var group in item.Index.Groups.ToArray().Randomize())
                            {
                                if (_existManager.GetCount(group) >= group.InformationLength) continue;

                                foreach (var key in _existManager.GetKeys(group, false))
                                {
                                    if (_connectionsManager.IsDownloadWaiting(key))
                                    {
                                        limitCount--;

                                        if (limitCount <= 0) goto End;
                                    }
                                }
                            }

                            foreach (var group in item.Index.Groups.ToArray().Randomize())
                            {
                                if (_existManager.GetCount(group) >= group.InformationLength) continue;

                                var tempKeys = new List<Key>();

                                foreach (var key in _existManager.GetKeys(group, false))
                                {
                                    if (!_connectionsManager.IsDownloadWaiting(key))
                                    {
                                        tempKeys.Add(key);
                                    }
                                }

                                random.Shuffle(tempKeys);
                                foreach (var key in tempKeys)
                                {
                                    _connectionsManager.Download(key);

                                    limitCount--;
                                }

                                if (limitCount <= 0) goto End;
                            }

                            End:;
                        }
                        else
                        {
                            item.State = DownloadState.ParityDecoding;
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = DownloadState.Error;

                    Log.Error(e);
                }
            }
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:101,代码来源:DownloadManager.cs


注:本文中的System.Random.Shuffle方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。