Top-rated recipes tagged "random"http://code.activestate.com/recipes/tags/random/top/2016-11-27T07:10:56-08:00ActiveState Code RecipesTicTacToe (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>
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>
Secure Password Generator (Python)
2011-06-17T15:25:20-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577759-secure-password-generator/
<p style="color: grey">
Python
recipe 577759
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>A password generator that uses OS facilities to generate none pseudo random numbers.
the SystemRandom uses CryptGenRandom in Windows and /dev/random in linux and it is so better to use this to create random numbers in cryptography or any other security areas.</p>
Pick random elements from a predefined list of choices (Python)
2011-06-14T17:46:41-07:00Patrick Dobbshttp://code.activestate.com/recipes/users/4177984/http://code.activestate.com/recipes/577754-pick-random-elements-from-a-predefined-list-of-cho/
<p style="color: grey">
Python
recipe 577754
by <a href="/recipes/users/4177984/">Patrick Dobbs</a>
(<a href="/recipes/tags/choice/">choice</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/selection/">selection</a>).
</p>
<p>This (quick but strangely satisfying) recipe combines the use of random.choice with functools.partial from the standard library. It is a factory function returning random.choice pre-filled with its sequence of options.</p>
Simple Sudoku (Python)
2011-06-02T14:15:02-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577716-simple-sudoku/
<p style="color: grey">
Python
recipe 577716
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/oo/">oo</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sudoku/">sudoku</a>).
Revision 10.
</p>
<p>This is a simple sudoku game.</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 Passwords (Python)
2010-07-27T20:28:17-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577339-random-passwords/
<p style="color: grey">
Python
recipe 577339
by <a href="/recipes/users/4174445/">Danillo Souza</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>).
</p>
<p>Generate strong random passwords of specified length.</p>
The 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>
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>
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>
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>
Dynamical Billiards Simulation (Python)
2013-06-20T06:57:57-07:00Steve Wadleyhttp://code.activestate.com/recipes/users/4186942/http://code.activestate.com/recipes/578572-dynamical-billiards-simulation/
<p style="color: grey">
Python
recipe 578572
by <a href="/recipes/users/4186942/">Steve Wadley</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>
Small random number generator (C)
2012-05-15T19:49:34-07:00Sachin Joglekarhttp://code.activestate.com/recipes/users/4181845/http://code.activestate.com/recipes/578134-small-random-number-generator/
<p style="color: grey">
C
recipe 578134
by <a href="/recipes/users/4181845/">Sachin Joglekar</a>
(<a href="/recipes/tags/generate/">generate</a>, <a href="/recipes/tags/malloc/">malloc</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/stdlib/">stdlib</a>).
</p>
<p>Generates random numbers in a given range using malloc function in stdlib.h. Based on the observation that the memory address allocated during malloc is usually 'random' (for humans). takes in two arguments- start of range, end of range.</p>
Random user generator (Python)
2012-02-27T16:58:33-08:00Koppula Varun Rajhttp://code.activestate.com/recipes/users/4181072/http://code.activestate.com/recipes/578055-random-user-generator/
<p style="color: grey">
Python
recipe 578055
by <a href="/recipes/users/4181072/">Koppula Varun Raj</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>I've worked on a random user generator in C a long time ago, so I thought I'd make an advanced version of it in Python(3.2). I'll have you know, I am an amateur in Python and even programming for that matter so I'd really appreciate some criticism on my code and what more can be added.</p>
<p>About the program - I've added a default list of users, you can modify the list to your convenience and for data persistence, I made use of the pickle module. The random module plays the pivotal role generating the output making use of the choice method. Those along with the os module if the program is being run for the first time on a pc, it will create a data file with the default list.After some digging around, I managed to find os.getlogin() from the python library which was exactly what I was looking for (to get the computer's name making sure it can get the data file to the correct directory). I'm a little skeptical about that part, wondering whether or not it will work on every pc.</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>
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>
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>
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>
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>