{"id":79992,"date":"2023-01-21T08:00:33","date_gmt":"2023-01-21T07:00:33","guid":{"rendered":"https:\/\/code-maze.com\/?p=79992"},"modified":"2023-01-21T11:25:12","modified_gmt":"2023-01-21T10:25:12","slug":"csharp-randomize-list","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-randomize-list\/","title":{"rendered":"How to Randomize a List in C#"},"content":{"rendered":"<p>C# has several built-in methods for working with arrays and lists, but what if you need to randomize a list of items? In this article, we&#8217;ll look at some techniques we can use to randomize <a href=\"https:\/\/code-maze.com\/csharp-list-collection\/\" target=\"_blank\" rel=\"noopener\">lists<\/a> in C#. We&#8217;ll also discuss some of the pros and cons of each technique and assess how they perform.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/collections-lists\/RandomizeGenericListsInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Without further ado, let&#8217;s start!<\/p>\n<h2><a id=\"prerequisites\"><\/a>Prerequisites<\/h2>\n<p>First, let&#8217;s define a function to generate a list of integers. We&#8217;ll use this when testing the different ways to randomize lists. The method accepts the list&#8217;s length as a parameter and returns a <code>List&lt;int&gt;<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;int&gt; OrderedInts(int size)\r\n{\r\n    var numbers = new List&lt;int&gt;();\r\n\r\n    for (int i = 0; i &lt; size; i++)\r\n    {\r\n        numbers.Add(i);\r\n    }\r\n\r\n    return numbers;\r\n}<\/pre>\n<p>Next, we are going to initialize a <code>List&lt;int&gt;<\/code> object containing 1,000,000 numbers in our test class, which we intend to shuffle using the strategies we discuss:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private readonly RandomizeGenericListsMethods _randomizeListObj;\r\nprivate readonly List&lt;int&gt; _orderedList;\r\n\r\npublic RandomizeGenericListsUnitTests()\r\n{\r\n    _randomizeListObj = new RandomizeGenericListsMethods();\r\n    _orderedList = _randomizeListObj.OrderedInts(1000000);\r\n}<\/pre>\n<p>Let&#8217;s dive into some of the techniques we can use to shuffle <code>List&lt;T&gt;<\/code> objects in C#.<\/p>\n<h2><a id=\"how-to-randomize-lists-loop\"><\/a>Fisher-Yates Shuffle Algorithm<\/h2>\n<p>This algorithm has both original and modern implementations. It&#8217;s worth familiarizing yourself with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fisher%E2%80%93Yates_shuffle\" target=\"_blank\" rel=\"nofollow noopener\">Fisher-Yates Shuffle<\/a>.<\/p>\n<p>We are going to implement the <strong>modern implementation of the Fisher-Yates Shuffle algorithm<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Assuming we have an array num of n elements:\r\nfor i from n\u22121 iterating down to 1 do\r\n     k \u2190 random integer that is 0 \u2264 j \u2264 i\r\n     swap num[k] with num[i]<\/pre>\n<p>When implementing this algorithm, we are going to start from the last element and swap it with a random element that we pick from the entire list. Then we proceed to reduce the size of the list by one and repeat the same process until we get to the first element.<\/p>\n<p>First, let&#8217;s define and initialize an instance of the <code>Random<\/code> class for use in our examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private readonly Random _rand;\r\n\r\npublic RandomizeGenericListsMethods() \r\n{\r\n    _rand = new Random();\r\n}<\/pre>\n<p>Next, let&#8217;s proceed to implement our algorithm:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;int&gt; GenerateRandomLoop(List&lt;int&gt; listToShuffle)\r\n{\r\n    for (int i = listToShuffle.Count - 1; i &gt; 0; i--)\r\n    {\r\n        var k = _rand.Next(i + 1);\r\n        var value = listToShuffle[k];\r\n        listToShuffle[k] = listToShuffle[i];\r\n        listToShuffle[i] = value;\r\n    }\r\n\r\n    return listToShuffle;\r\n}<\/pre>\n<p>The algorithm has <strong>an O(N) time complexity as its runtime depends on the size of the list<\/strong>. We also assume that the <code>Random<\/code> class <strong>takes O(1) time to generate a number<\/strong>.\u00a0<\/p>\n<p>Finally, we can verify that we can successfully shuffle our <code>List&lt;int&gt;<\/code> object using the Fisher-Yates algorithm:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var shuffledList = _randomizeListObj.GenerateRandomLoop(_orderedList);\r\n\r\nvar firstVal = shuffledList.First();\r\nvar lastVal = shuffledList.Last();\r\n\r\nAssert.AreNotEqual(firstVal, 0);\r\nAssert.AreNotEqual(lastVal, 999999);<\/pre>\n<p>Alternatively, we can leverage existing libraries such as <a href=\"https:\/\/github.com\/morelinq\/MoreLINQ\" target=\"_blank\" rel=\"nofollow noopener\">MoreLINQ<\/a> to randomize our lists. The library has a <code>Shuffle()<\/code> method, which implements the Fisher-Yates algorithm to help us shuffle our lists.\u00a0<\/p>\n<h2><a id=\"how-to-randomize-lists-orderby-random\"><\/a>Randomize a List using OrderBy Random Numbers<\/h2>\n<p>We can use the inbuilt <a href=\"https:\/\/code-maze.com\/csharp-random-class\/\" target=\"_blank\" rel=\"noopener\">random class<\/a> in C# to shuffle a <code>List&lt;T&gt;<\/code> object in C# by invoking it with the <code>OrderBy()<\/code> method in a lambda expression.<\/p>\n<p>To make our example simple, let&#8217;s define a method that accepts and randomizes a <code>List&lt;int&gt;<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;int&gt; GenerateRandomOrderBy(List&lt;int&gt; listToShuffle)\r\n{\r\n    var shuffledList = listToShuffle.OrderBy(_ =&gt; _rand.Next()).ToList();\r\n\r\n    return shuffledList;\r\n}<\/pre>\n<p>The method invokes the <code>OrderBy()<\/code> and <code>rand.Next()<\/code> functions to shuffle the <code>listToShuffle<\/code> object by ordering it by random numbers.\u00a0<\/p>\n<p>Next, we can verify that our method successfully shuffles a list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var shuffledList = _randomizeListObj.GenerateRandomOrderBy(_orderedList);\r\n\r\nvar firstVal = shuffledList.First();\r\nvar lastVal = shuffledList.Last();\r\n\r\nAssert.AreNotEqual(firstVal, 0);\r\nAssert.AreNotEqual(lastVal, 999999);<\/pre>\n<h2><a id=\"shuffle-by-guid\"><\/a>Shuffle a List using OrderBy by Guid Values\u00a0<\/h2>\n<p>We use a similar strategy as the previous <a href=\"#how-to-randomize-lists-orderby-random\">example<\/a>, except that we substitute the <code>Random<\/code> class with the <code>Guid.NewGuid()<\/code> method in our <code>OrderBy()<\/code> clause:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;int&gt; GenerateRandomOrderByGuid(List&lt;int&gt; listToShuffle)\r\n{\r\n    var shuffledList = listToShuffle.OrderBy(_ =&gt; Guid.NewGuid()).ToList();\r\n\r\n    return shuffledList;\r\n}<\/pre>\n<p>Let&#8217;s verify that we can shuffle our <code>List&lt;int&gt;<\/code> object with this strategy:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var shuffledList = _randomizeListObj.GenerateRandomOrderByGuid(_orderedList);\r\n\r\nvar firstVal = shuffledList.First();\r\nvar lastVal = shuffledList.Last();\r\n\r\nAssert.AreNotEqual(firstVal, 0);\r\nAssert.AreNotEqual(lastVal, 999999);     <\/pre>\n<h2><a id=\"performance-benchmarks\"><\/a>Performance Benchmarks<\/h2>\n<p>First, we are going to create a new object that generates a <code>List&lt;int&gt;<\/code> object to shuffle when performing these benchmarks:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public IEnumerable&lt;List&lt;int&gt;&gt; SampleList()\r\n{\r\n    yield return OrderedInts(1000000);\r\n}<\/pre>\n<p>We use the <a href=\"https:\/\/code-maze.com\/csharp-async-enumerable-yield\/#yield\" target=\"_blank\" rel=\"noopener\">yield<\/a>\u00a0operator in our example to create the list on demand, which has some performance benefits.\u00a0<\/p>\n<p>Next, let&#8217;s analyze our benchmark results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">|                    Method |      Mean |     Error |    StdDev | Rank |  Allocated |\r\n|-------------------------- |----------:|----------:|----------:|-----:|-----------:|\r\n|        GenerateRandomLoop |  24.92 ms |  1.645 ms |  4.798 ms |    1 |       24 B |\r\n|     GenerateRandomOrderBy | 428.39 ms | 21.995 ms | 64.506 ms |    2 | 16000752 B |\r\n| GenerateRandomOrderByGuid | 699.60 ms | 28.362 ms | 79.994 ms |    3 | 28000688 B |<\/pre>\n<p>We can see that the <strong>Fisher-Yates algorithm is almost twenty times faster than the other Linq-based functions<\/strong>. The <code>OrderBy()<\/code> method implements the <a href=\"https:\/\/code-maze.com\/csharp-quicksort-algorithm\/\" target=\"_blank\" rel=\"noopener\">quicksort algorithm<\/a>, which has a worst-case complexity of <strong>O(N<sup>2<\/sup>)\u00a0<\/strong>while our Fisher-Yates algorithm implementation takes <strong>O(N)<\/strong>.<\/p>\n<p>Our <code>Guid<\/code> solution seems to be almost one and a half times slower than our <code>Random<\/code> solution because <code>Guid<\/code> values consume more memory, which affects how the algorithm performs.<\/p>\n<h2><a id=\"benefits\"><\/a>What Are Some Benefits of Randomizing Lists?<\/h2>\n<p>First, creating random lists in C# helps us to easily <strong>create dynamic and flexible content for our applications<\/strong>.<\/p>\n<p>Additionally, randomizing lists allows us to create <strong>more engaging user experiences by keeping things interesting and unpredictable<\/strong>.<\/p>\n<p>Furthermore, this approach offers a number of performance benefits, since it doesn&#8217;t require us to store and manipulate large amounts of data.<\/p>\n<h2><a id=\"drawbacks\"><\/a>Drawbacks of Creating Random Lists<\/h2>\n<p>One of the drawbacks of randomly generating lists is that there&#8217;s a <strong>tendency for the same elements to be repeated more often than would be ideal<\/strong>. This could potentially lead to <strong>suboptimal results when using these lists in applications<\/strong>, as certain elements will appear more frequently than others.<\/p>\n<h2>Conclusion\u00a0<\/h2>\n<p>In this article, we learn how to randomize a list in C#. We can see that there are a few different ways to do this, and each has its benefits and drawbacks. We are looking forward to getting your feedback and learning together!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# has several built-in methods for working with arrays and lists, but what if you need to randomize a list of items? In this article, we&#8217;ll look at some techniques we can use to randomize lists in C#. We&#8217;ll also discuss some of the pros and cons of each technique and assess how they perform. [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[366,1588,973],"class_list":["post-79992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-generics","tag-lists","tag-random","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Randomize a List in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we&#039;ll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-randomize-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Randomize a List in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we&#039;ll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-randomize-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-21T07:00:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-21T10:25:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"How to Randomize a List in C#\",\"datePublished\":\"2023-01-21T07:00:33+00:00\",\"dateModified\":\"2023-01-21T10:25:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/\"},\"wordCount\":737,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"generics\",\"lists\",\"Random\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-randomize-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/\",\"url\":\"https:\/\/code-maze.com\/csharp-randomize-list\/\",\"name\":\"How to Randomize a List in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-01-21T07:00:33+00:00\",\"dateModified\":\"2023-01-21T10:25:12+00:00\",\"description\":\"In this article, we'll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-randomize-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-randomize-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Randomize a List in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Randomize a List in C# - Code Maze","description":"In this article, we'll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-randomize-list\/","og_locale":"en_US","og_type":"article","og_title":"How to Randomize a List in C# - Code Maze","og_description":"In this article, we'll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.","og_url":"https:\/\/code-maze.com\/csharp-randomize-list\/","og_site_name":"Code Maze","article_published_time":"2023-01-21T07:00:33+00:00","article_modified_time":"2023-01-21T10:25:12+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"How to Randomize a List in C#","datePublished":"2023-01-21T07:00:33+00:00","dateModified":"2023-01-21T10:25:12+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/"},"wordCount":737,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["generics","lists","Random"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-randomize-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-randomize-list\/","url":"https:\/\/code-maze.com\/csharp-randomize-list\/","name":"How to Randomize a List in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-01-21T07:00:33+00:00","dateModified":"2023-01-21T10:25:12+00:00","description":"In this article, we'll look at some techniques we can use to randomize a list in C#, as well as cover some of the pros and cons.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-randomize-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-randomize-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Randomize a List in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=79992"}],"version-history":[{"count":1,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79992\/revisions"}],"predecessor-version":[{"id":79993,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79992\/revisions\/79993"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=79992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=79992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=79992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}