{"id":5658,"date":"2021-11-19T09:01:19","date_gmt":"2021-11-19T08:01:19","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5658"},"modified":"2025-02-06T14:59:30","modified_gmt":"2025-02-06T13:59:30","slug":"c-get-random-numbers","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-get-random-numbers\/","title":{"rendered":"C# Get random items or numbers"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System.Security.Cryptography;\n\npublic class RandomHelper\n{\n    public static double GetRandomDouble()\n    {\n        var bytes = new byte[8];\n        RandomNumberGenerator.Fill(bytes);\n        var randomUInt64 = BitConverter.ToUInt64(bytes, 0);\n        \/\/ Schaal naar [0.0, 1.0) door te delen door (ulong.MaxValue + 1.0)\n        return randomUInt64 \/ ((double)ulong.MaxValue + 1.0);\n    }\n\n    public static string GetRandomChar(string charString) => $\"{GetRandom(charString.ToCharArray())}\";\n\n    public static T GetRandom&lt;T>(params T[] values)\n    {\n        if (values == null || values.Length == 0)\n            throw new ArgumentException(\"The values array cannot be null or empty.\", nameof(values));\n        return values[GetRandomInt(values.Length)];\n    }\n\n    public static int GetRandomInt(int maximum) => GetRandomInt(0, maximum);\n    public static int GetRandomInt(int minimum, int maximum)\n    {\n        if (minimum == maximum) return minimum;\n        if (minimum > maximum)\n            throw new ArgumentOutOfRangeException(nameof(minimum), \"Minimum should be less than or equal to maximum.\");\n        return RandomNumberGenerator.GetInt32(minimum, maximum);\n    }\n\n    public static double GetRandomDouble(double minimum, double maximum)\n    {\n        if (minimum == maximum) return minimum;\n        if (minimum > maximum)\n            throw new ArgumentOutOfRangeException(nameof(minimum), \"Minimum should be less than or equal to maximum.\");\n\n        return GetRandomDouble() * (maximum - minimum) + minimum;\n    }\n    \n    public static T GetRandomItem&lt;T>(this IEnumerable&lt;T> self) => self.Skip(RandomNumberGenerator.GetInt32(0, self.Count() - 1)).FirstOrDefault();\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-5658","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5658"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5658\/revisions"}],"predecessor-version":[{"id":9263,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5658\/revisions\/9263"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}