by
84k 18
1
4
2,226
4
Top 1% !
Famous
Tagged
Specified
Popularity: 10773rd place

Published on:

Architecturex86_64
LanguageC#
Windows VersionWindows7

Random: Returns a random symbol within the given symbol probabilities.

Returns a random symbol within the given symbol probabilities.

 
<param name="probabilities">The probabilities for the discrete symbols.</param>
 
<returns>A random symbol within the given probabilities.</returns>
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1456193/Random--Returns-a-random-symbol-within-t?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/// <summary> /// Returns a random symbol within the given symbol probabilities. /// </summary> /// /// <param name="probabilities">The probabilities for the discrete symbols.</param> /// /// <returns>A random symbol within the given probabilities.</returns> /// public static int Random(double[] probabilities) { double uniform = Accord.Math.Random.Generator.Random.NextDouble(); double cumulativeSum = 0; // Use the probabilities to partition the [0,1] interval // and check inside which range the values fall into. for (int i = 0; i < probabilities.Length; i++) { cumulativeSum += probabilities[i]; if (uniform < cumulativeSum) return i; } throw new InvalidOperationException("Generated value is not between 0 and 1."); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus