Popular Python recipes tagged "random_number"http://code.activestate.com/recipes/langs/python/tags/random_number/2016-07-17T17:26:47-07:00ActiveState Code RecipesThe many uses of randomness - Part 2 (Python)
2016-07-17T17:26:47-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580690-the-many-uses-of-randomness-part-2/
<p style="color: grey">
Python
recipe 580690
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/testing/">testing</a>).
</p>
<p>This is the second recipe in the series about the uses of randomness in Python. The first recipe is here:</p>
<p><a href="https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351</a></p>
<p>This second recipe shows some uses of random numbers to generate random characters and random strings of various categories, and some purposes for which these generated strings can be used in testing.</p>
The many uses of randomness - Part 1 (Python)
2016-06-01T19:36:28-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/
<p style="color: grey">
Python
recipe 580674
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>).
</p>
<p>This recipe shows some of the many uses of random numbers, using the random function from the random module from Python's standard library. A subsequent recipe or two will show other uses, both of other functions from the module, and for other purposes.</p>
<p>The uses shown in this recipe have to do with using random float values, and scaling them and offsetting them, and also how to get a repeated/predictable series of random numbers.</p>
FeatureClassRandomizer (Python)
2012-08-07T15:35:23-07:00Dale Lindemanhttp://code.activestate.com/recipes/users/4183096/http://code.activestate.com/recipes/578237-featureclassrandomizer/
<p style="color: grey">
Python
recipe 578237
by <a href="/recipes/users/4183096/">Dale Lindeman</a>
(<a href="/recipes/tags/randomization/">randomization</a>, <a href="/recipes/tags/random_number/">random_number</a>).
</p>
<p>Generates a randomized list of unique IDs and iteratively assigns values to features in a featureclass.</p>
Walker's alias method for random objects with different probablities (Python)
2008-11-16T15:05:51-08:00denishttp://code.activestate.com/recipes/users/4168005/http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/
<p style="color: grey">
Python
recipe 576564
by <a href="/recipes/users/4168005/">denis</a>
(<a href="/recipes/tags/random_number/">random_number</a>, <a href="/recipes/tags/walker_s_alias/">walker_s_alias</a>).
</p>
<p>an example, strings A B C or D with probabilities .1 .2 .3 .4 --</p>
<pre class="prettyprint"><code>abcd = dict( A=1, D=4, C=3, B=2 )
# keys can be any immutables: 2d points, colors, atoms ...
wrand = Walkerrandom( abcd.values(), abcd.keys() )
wrand.random() # each call -> "A" "B" "C" or "D"
# fast: 1 randint(), 1 uniform(), table lookup
</code></pre>
Generating random numbers with arbitrary distribution (Python)
2008-11-05T07:52:39-08:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576556-generating-random-numbers-with-arbitrary-distribut/
<p style="color: grey">
Python
recipe 576556
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>).
</p>
<p>This is a class that allows you to set up an arbitrary probability distribution function and generate random numbers that follow that arbitrary distribution.</p>
Generating correlated random numbers (Python)
2008-09-21T21:21:52-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576512-generating-correlated-random-numbers/
<p style="color: grey">
Python
recipe 576512
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/random_number/">random_number</a>).
</p>
<p>From this great <a href="http://www.sitmo.com/doc/Generating_Correlated_Random_Numbers">tutorial</a></p>
<p>For two corelated variables, the formula is much as one would get from intuition about the meaning of correlation with some twist due to normalizing the standard deviation:
$X_3 = \alpha X_1 + \sqrt{1-\alpha^2} X_2$
Where $X_1$ and $X_2$ are two independent random variables, and $\alpha$ is the coefficient of correlation between $X_1$ and $X_3$.</p>
<p>In a more general sense: <br />
Let $C$ be the correlation matrix desired. Let $X_1, X_2..., X_N$ be $N$ independent random variables arranged in a row matrix $R = [X_1, X_2,....,X_N]$. Then
$Q = RU$
where
$U^TU = C$
gives us $N$ random variables $Q = [Y_1, Y_2, ..., Y_N]$ with the required property.</p>