Most viewed recipes tagged "random"http://code.activestate.com/recipes/tags/random/views/2016-11-27T07:10:56-08:00ActiveState Code RecipesLong period random number generator (Python) 2010-12-02T05:19:25-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576707-long-period-random-number-generator/ <p style="color: grey"> Python recipe 576707 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/random/">random</a>). Revision 13. </p> <p>Implements a complementary-multiply-with-carry psuedo-random-number-generator. Period is 3636507990 * 2 ** 43487 (approximately 10 ** 13101).</p> Numerical Integration using Monte Carlo method (Python) 2010-06-16T19:31:17-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577263-numerical-integration-using-monte-carlo-method/ <p style="color: grey"> Python recipe 577263 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>Numerical Integration using Monte Carlo method.</p> TicTacToe (text based) (Python) 2016-11-27T07:10:56-08:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580723-tictactoe-text-based/ <p style="color: grey"> Python recipe 580723 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/tac/">tac</a>, <a href="/recipes/tags/tic/">tic</a>, <a href="/recipes/tags/tictactoe/">tictactoe</a>, <a href="/recipes/tags/toe/">toe</a>). </p> <p>A text based version of TicTacToe for Python. Difficulty is not yet implemented, however, a computer player has been implemented that cannot be beaten. Posting just for fun.</p> State Capitals Quiz (Python) 2009-05-28T07:45:06-07:00joedaviscpahttp://code.activestate.com/recipes/users/4170361/http://code.activestate.com/recipes/576783-state-capitals-quiz/ <p style="color: grey"> Python recipe 576783 by <a href="/recipes/users/4170361/">joedaviscpa</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>The program loops through all 50 states asking for the state capital. </p> Random Binary List (Python) 2011-11-09T19:38:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577944-random-binary-list/ <p style="color: grey"> Python recipe 577944 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_binary_list/">random_binary_list</a>). </p> <p>This recipe returns a list of size n such that the contents of the returned list are random 0s and 1s. It returns a random binary list of size n.</p> Random numbers with arbitrary probability distribution. (Python) 2010-06-14T17:02:41-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577264-random-numbers-with-arbitrary-probability-distribu/ <p style="color: grey"> Python recipe 577264 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>Generating N random numbers that probability distribution fits to any given function curve.</p> Generate a set of random integers (Python) 2016-02-18T19:34:17-08:00Lance Spencehttp://code.activestate.com/recipes/users/4193647/http://code.activestate.com/recipes/580613-generate-a-set-of-random-integers/ <p style="color: grey"> Python recipe 580613 by <a href="/recipes/users/4193647/">Lance Spence</a> (<a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>This is just a short and simple script that generates a set of random numbers. You enter how many random numbers you want and the program will generate them.</p> <p>The num is multiplied by 10 in the upper limit within the for loop to give you larger generated integers. Adjust it as needed.</p> Area Under the Curve using Monte Carlo method (Python) 2010-06-17T03:54:27-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577262-area-under-the-curve-using-monte-carlo-method/ <p style="color: grey"> Python recipe 577262 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 5. </p> <p>Calculating area under the curve using Monte Carlo method for any given function.</p> Guess a number (Python) 2014-11-15T09:06:05-08:00Benoithttp://code.activestate.com/recipes/users/4191135/http://code.activestate.com/recipes/578962-guess-a-number/ <p style="color: grey"> Python recipe 578962 by <a href="/recipes/users/4191135/">Benoit</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sample/">sample</a>). </p> <p>This is just a little game to explain some very basic function in Python for beginner. RANDOM, Loop, Try and except... </p> Dynamical Billiards Simulation (Python) 2010-10-30T06:22:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577445-dynamical-billiards-simulation/ <p style="color: grey"> Python recipe 577445 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>It simulates reflections of a ball on a billiards table that has one or more circular obstacles. (This can also be thought as a 2d ray-tracing.)</p> <p>Most of the time the path of the ball would be chaotic (meaning, if another ball started from any slightly different location or direction then its path would be very different after a short while). </p> <p>See Wikipedia for more info: <a href="http://en.wikipedia.org/wiki/Dynamical_billiards" rel="nofollow">http://en.wikipedia.org/wiki/Dynamical_billiards</a></p> Random Password Generation (Python) 2014-08-10T15:50:40-07:00Paul Wolfhttp://code.activestate.com/recipes/users/4190553/http://code.activestate.com/recipes/578920-random-password-generation/ <p style="color: grey"> Python recipe 578920 by <a href="/recipes/users/4190553/">Paul Wolf</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/strong/">strong</a>). </p> <p>Generate a password (or other secure token) using a pattern language similar to regular expressions. We'll use the <code>strgen</code> module that enables a user to generate test data, unique ids, passwords, vouchers or other randomized data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings.</p> Pseudo-Random Experiment: Coin Toss (Python) 2014-03-31T18:51:15-07:00Fabian Mayerhttp://code.activestate.com/recipes/users/4189629/http://code.activestate.com/recipes/578859-pseudo-random-experiment-coin-toss/ <p style="color: grey"> Python recipe 578859 by <a href="/recipes/users/4189629/">Fabian Mayer</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/toss/">toss</a>). </p> <p>This Python Script flips a coin a user defined number of times and returns the frequency and relative frequency for heads and tails.</p> Calculating PI using random numbers (Monte Carlo method) (Python) 2010-03-27T00:58:24-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577154-calculating-pi-using-random-numbers-monte-carlo-me/ <p style="color: grey"> Python recipe 577154 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>Calculating PI using random numbers (Monte Carlo method)</p> 3d Surface fitting to N random points (Python) 2010-03-16T20:55:31-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577119-3d-surface-fitting-to-n-random-points/ <p style="color: grey"> Python recipe 577119 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>3d Surface fitting to N random points. It draws the surface by converting z values to RGB colors. </p> Examples for random float between 0 and 1 (Python) 2015-06-25T17:55:33-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/579072-examples-for-random-float-between-0-and-1/ <p style="color: grey"> Python recipe 579072 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>I was researching how floats are stored in a computer and was trying to think up a way to generate random values between 0 and 1. Python already provides an implementation allowing this already, and two of the functions below are directly inspired by that code, but the third is a slightly different way of doing the same thing. A similar version of this code has been used to implement similar functionality in C# at one time. Others might also find it useful if they want to create equivalent code in a separate language while having access to random bytes but not random floats. It should be noted that the various implementations get slower as you go down the list.</p> Funny text generator (Python) 2011-12-25T11:14:39-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/577988-funny-text-generator/ <p style="color: grey"> Python recipe 577988 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/generation/">generation</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>This class takes a text (preferably long enough) and generates another text that "looks like" the original. It won't mean anything, or just by chance ;-)</p> <p>For example, taking Hamlet, Act I, the program generates things like :</p> <p>Hamlet</p> <blockquote> <p>And vanish'd from our watch; <br /> His further. Fare third nights of the flushing immortal as it draw you into the flushing thy complete steel <br /> 'Tis sweet and each new-hatch'd: <br /> A country's father; <br /> To business and is prodigal thee! <br /> Have of crowing more the should I have heaven, <br /> Forward, therefore as ourself in the business it, Horatio <br /> To what is't that your watch, bid this here! </p> </blockquote> <p>Usage :</p> <pre class="prettyprint"><code>generator = TextGenerator(txt) result = generator.random_text(3000) </code></pre> Julia fractals (Python) 2010-03-26T04:47:44-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577120-julia-fractals/ <p style="color: grey"> Python recipe 577120 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>It draws a randomly selected Julia fractal set each time.</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> random send mail, sms or popup window (Python) 2013-09-04T19:43:19-07:00peekaahttp://code.activestate.com/recipes/users/2919471/http://code.activestate.com/recipes/578598-random-send-mail-sms-or-popup-window/ <p style="color: grey"> Python recipe 578598 by <a href="/recipes/users/2919471/">peekaa</a> (<a href="/recipes/tags/popup/">popup</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/sms/">sms</a>). Revision 4. </p> <p>If you need to do something irregularly, randomly during the day, you often forget. This script gives you mail, sms or popup window indefinitely in random interval to remind you of doing it. It runs forever. If you want to send emails, uncomment the row sendMail() and fill variable me, to, smtp, name, login in function sendMail().</p> Weighted random choice (Python) 2010-08-19T08:40:38-07:00Carlos Valientehttp://code.activestate.com/recipes/users/4174637/http://code.activestate.com/recipes/577363-weighted-random-choice/ <p style="color: grey"> Python recipe 577363 by <a href="/recipes/users/4174637/">Carlos Valiente</a> (<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sequence/">sequence</a>). </p> <p>This function returns a random element from a sequence. The probability for each element in the sequence to be selected can be weighted by a user-provided callable</p>