<p>I would like to generate a random floating point number between 2 values.  What is the best way to do this in C#?</p>

[{'answer_id': 44424, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 2, 'selected': False, 'text': '<pre><code>// generate a random number starting with 5 and less than 15\nRandom r = new Random();\nint num = r.Next(5, 15);  \n</code></pre>\n\n<p>For doubles you can replace Next with NextDouble</p>\n'}, {'answer_id': 44428, 'author': 'Eric', 'author_id': 4540, 'author_profile': 'https://Stackoverflow.com/users/4540', 'pm_score': 4, 'selected': False, 'text': '<pre><code>System.Random r = new System.Random();\n\ndouble rnd( double a, double b )\n{\n   return a + r.NextDouble()*(b-a);\n}\n</code></pre>\n'}, {'answer_id': 44430, 'author': 'enigmatic', 'author_id': 443575, 'author_profile': 'https://Stackoverflow.com/users/443575', 'pm_score': 1, 'selected': False, 'text': '<p>How random?  If you can deal with pseudo-random then simply:</p>\n\n<pre><code>Random randNum = new Random();\nrandNum. NextDouble(Min, Max);\n</code></pre>\n\n<p>If you want a "better" random number, then you probably should look at the Mersenne Twister algorithm.  Plenty of people hav <a href="http://code.msdn.microsoft.com/MersenneTwister" rel="nofollow noreferrer">already implemented it</a> for you though</p>\n'}, {'answer_id': 44461, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 7, 'selected': True, 'text': '<p>The only thing I\'d add to <a href="https://stackoverflow.com/questions/44408/how-do-you-generate-a-random-number-in-c#44428">Eric</a>\'s response is an explanation; I feel that knowledge of why code works is better than knowing what code works.</p>\n\n<p>The explanation is this: let\'s say you want a number between 2.5 and 4.5.  The range is 2.0 (4.5 - 2.5).  <code>NextDouble</code> only returns a number between 0 and 1.0, but if you multiply this by the range you will get a number between 0 and <em>range</em>.</p>\n\n<p>So, this would give us random doubles between 0.0 and 2.0:</p>\n\n<pre>rng.NextDouble() * 2.0</pre>\n\n<p>But, we want them between 2.5 and 4.5!  How do we do this?  Add the smallest number, 2.5:</p>\n\n<pre>2.5 + rng.NextDouble() * 2.0</pre>\n\n<p>Now, we get a number between 0.0 and 2.0; if you add 2.5 to each of these values we see that the range is now between 2.5 and 4.5.</p>\n\n<p>At first I thought that it mattered if b > a or a > b, but if you work it out both ways you\'ll find it works out identically so long as you don\'t mess up the order of the variables used.  I like to express it with longer variable names so I don\'t get mixed up:</p>\n\n<pre>double NextDouble(Random rng, double min, double max)\n{\n    return min + (rng.NextDouble() * (max - min));\n}</pre>\n'}, {'answer_id': 44472, 'author': 'Sameer Alibhai', 'author_id': 2343, 'author_profile': 'https://Stackoverflow.com/users/2343', 'pm_score': 1, 'selected': False, 'text': '<p>Here is a snippet of how to get Cryographically safe random numbers:\nThis will fill in the 8 bytes with a crytographically strong sequence of random values.</p>\n\n<pre><code>byte[] salt = new byte[8];\nRNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();\nrng.GetBytes(salt);\n</code></pre>\n\n<p>For more details see <a href="http://www.sharpdeveloper.net/content/archive/2007/12/05/how-random-is-your-random.aspx" rel="nofollow noreferrer">How Random is your Random??"</a> (inspired by a CodingHorror article on deck shuffling)</p>\n'}, {'answer_id': 44501, 'author': 'Andrew Burns', 'author_id': 3683, 'author_profile': 'https://Stackoverflow.com/users/3683', 'pm_score': 1, 'selected': False, 'text': '<p>For an explaination of why Longhorn has been downmodded so much: <a href="http://msdn.microsoft.com/en-us/magazine/cc163367.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163367.aspx</a> Look for the implementation of NextDouble and the explanation of what is a random double.</p>\n\n<p>That link is also a goo example of how to use cryptographic random numbers (like Sameer mentioned) only with actual useful outputs instead of a bit stream.</p>\n'}]
['https://Stackoverflow.com/questions/44408', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2064/']