Popular recipes tagged "meta:requires=random" and "algorithms"http://code.activestate.com/recipes/tags/meta:requires=random+algorithms/2012-04-22T06:45:43-07:00ActiveState Code RecipesCompare algorithms for heapq.smallest (Python)
2011-12-25T23:41:23-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest/
<p style="color: grey">
Python
recipe 577573
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/heaps/">heaps</a>, <a href="/recipes/tags/performance/">performance</a>).
Revision 3.
</p>
<p>General purpose technique for counting comparisons in various searching and sorting applications.</p>
Pollard Rho Prime Factorization (Python)
2010-02-10T10:02:51-08:00Mukesh Tiwarihttp://code.activestate.com/recipes/users/4172899/http://code.activestate.com/recipes/577037-pollard-rho-prime-factorization/
<p style="color: grey">
Python
recipe 577037
by <a href="/recipes/users/4172899/">Mukesh Tiwari</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 4.
</p>
<p>This code is implementation of Pollard Rho prime factorization. As i am a bit new in python so further improvement is appreciated.Also added Brent variant.</p>
Binary floating point summation accurate to full precision (Python)
2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/
<p style="color: grey">
Python
recipe 393090
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p>
Factorial-Lambda (Python)
2011-01-27T12:40:22-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/576616-factorial-lambda/
<p style="color: grey">
Python
recipe 576616
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>).
Revision 4.
</p>
<p>If n is a positive integer,factorial(n!) is the product of all the positive integers from 1 up to the given integer.
However, different types of functions are applied,including the anonymous lambda to generate the factorial number, because it is simple and returns a value ( a new function ), which can be assigned a name.</p>
Weighted random choice (Python)
2008-07-22T06:11:19-07:00Facundo Batistahttp://code.activestate.com/recipes/users/2016392/http://code.activestate.com/recipes/576370-weighted-random-choice/
<p style="color: grey">
Python
recipe 576370
by <a href="/recipes/users/2016392/">Facundo Batista</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Choices randomly an element from a list, not uniformly, but using a given weight for each element.</p>
SPICE (Python)
2008-04-19T20:08:36-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/572159-spice/
<p style="color: grey">
Python
recipe 572159
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This module provides access to a standardized implementation
of SPICE (Stephen's Power-Inspired, Computerized Encryption).</p>
<p>This code has just been revised and rewritten. As such it may
still contain bugs that were not found after testing the code.</p>
Iterator merge 2 (Python)
2007-12-07T22:16:05-08:00Jonathan Croninhttp://code.activestate.com/recipes/users/4107146/http://code.activestate.com/recipes/535160-iterator-merge-2/
<p style="color: grey">
Python
recipe 535160
by <a href="/recipes/users/4107146/">Jonathan Cronin</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Memory efficient multi-way iterator merge, without using heapq</p>
Friendly Readable ID Strings (Python)
2007-08-10T14:27:52-07:00Robin Parmarhttp://code.activestate.com/recipes/users/99666/http://code.activestate.com/recipes/526619-friendly-readable-id-strings/
<p style="color: grey">
Python
recipe 526619
by <a href="/recipes/users/99666/">Robin Parmar</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>One often needs unique ID strings to tag processes, threads, files or anything else that might be created in quantity. Traditionally these are created based on PIDs or similar system values. But it is not easy to visually recognise these strings, which makes debugging more difficult than it need be. This recipe creates readable and pronounceable ID strings that are much easier to work with.</p>
K fold cross validation partition (Python)
2007-06-16T08:57:51-07:00John Reidhttp://code.activestate.com/recipes/users/4023487/http://code.activestate.com/recipes/521906-k-fold-cross-validation-partition/
<p style="color: grey">
Python
recipe 521906
by <a href="/recipes/users/4023487/">John Reid</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Takes a sequence and yields K partitions of it into training and validation test sets. Training sets are of size (k-1)*len(X)/K and partition sets are of size len(X)/K</p>
Python Octree Implementation (Python)
2006-09-21T03:12:00-07:00Ben Harlinghttp://code.activestate.com/recipes/users/4000770/http://code.activestate.com/recipes/498121-python-octree-implementation/
<p style="color: grey">
Python
recipe 498121
by <a href="/recipes/users/4000770/">Ben Harling</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This is a simple implementation of an octree data structure in python. Its use is primarily for fast collision or view frustrum culling in interactive 3d environments, but its possible uses are quite open-ended. It was originally written for use with the pyOgre 3d engine binding. The code makes use of recursive functions to insert and find nodes in the octree, and is heavily commented. It can store any type of object you create, so long as that object has a 'position' property in the form of a 3-vector tuple. It includes a test function which relies on the random module, but the octree itself has no required dependencies. It will try to use the psyco module to speed up its execution, but that is not essential.</p>
Miller-Rabin Primality Test (Python)
2007-04-07T16:11:50-07:00Dite Ditehttp://code.activestate.com/recipes/users/4047667/http://code.activestate.com/recipes/511459-miller-rabin-primality-test/
<p style="color: grey">
Python
recipe 511459
by <a href="/recipes/users/4047667/">Dite Dite</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Very simple implementation of Miller-Rabin Primality Test with Tkinter</p>
Boids Demonstration (Python)
2007-02-12T21:34:08-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502216-boids-demonstration/
<p style="color: grey">
Python
recipe 502216
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This recipe demonstrates a 2D boids simulation.
The code is configurable based on some constants defined near the top.
The idea for the code shown here came from the following URL:
<a href="http://www.vergenet.net/%7Econrad/boids/pseudocode.html" rel="nofollow">http://www.vergenet.net/~conrad/boids/pseudocode.html</a></p>
Bouncing Ball Simulation (Python)
2007-02-20T19:04:03-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502241-bouncing-ball-simulation/
<p style="color: grey">
Python
recipe 502241
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>The physics system used in this simulation is
both incomplete and incorrect. This recipe evolved
from the Boids Simulation and was an early attempt
to simulate bouncing balls in an area contained
with a force field on the sides and a floor on the
bottom. The spheres will respond to movement of
the window and will bounce if accelerated upward
quickly. This is primarily a recipe that I later
built off of to create better software later on.</p>
Boids Version 1.1 (Python)
2007-02-20T18:53:31-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502240-boids-version-11/
<p style="color: grey">
Python
recipe 502240
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>The code presented below is an update to the original
recipe presented on this web site. Several more
configuration options are presented at the top of
the file, and Rule 3 from the Boids algorithm is
correctly used in the update_velocity method.</p>
Weighted choice (Python)
2006-10-30T13:48:55-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498229-weighted-choice/
<p style="color: grey">
Python
recipe 498229
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>To choose among a list of objects using a specified frequency distribution.</p>
Mastermind-style code-breaking (Python)
2012-04-22T06:45:43-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/496907-mastermind-style-code-breaking/
<p style="color: grey">
Python
recipe 496907
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Framework for experimenting with guessing strategies in Master-mind style games.</p>
Simple function base Genetic Algorithm (Python)
2006-11-09T18:53:29-08:00Ed Blakehttp://code.activestate.com/recipes/users/2492237/http://code.activestate.com/recipes/498257-simple-function-base-genetic-algorithm/
<p style="color: grey">
Python
recipe 498257
by <a href="/recipes/users/2492237/">Ed Blake</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This is a solution to the first problem in this tutorial:
<a href="http://www.ai-junkie.com/ga/intro/gat1.html" rel="nofollow">http://www.ai-junkie.com/ga/intro/gat1.html</a></p>
<p>I wrote it in about an hour, and tried to keep everything as clear and simple as possible. Please excuse the sparse commenting.</p>
<p>I deviated from the examples in the tutorial in several ways, the most important of which is the way the results are evaluated. In the tutorial it is stated that the equations are solved from left to right, but for expedience I let Python's operator precedence determine the order of evaluation. I'm not sure how close this example is to the example solution provided in the tutorial. I was to lazy to download unzip and decode the C source...</p>
Amaze - An object-oriented maze generator/solver in Python (Python)
2006-07-26T15:04:50-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/496884-amaze-an-object-oriented-maze-generatorsolver-in-p/
<p style="color: grey">
Python
recipe 496884
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 10.
</p>
<p>This is a simple maze generator & solver written in Python. It is written as a game, consisting of classes which can read mazes from STDIN or a file. It provides a a random maze generator game, which can generate mazes of any dimension and solve it. Use it for fun and learning.</p>
z_crypt.py (Python)
2006-07-03T05:44:03-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/496858-z_cryptpy/
<p style="color: grey">
Python
recipe 496858
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>The module show a way to simple data encryption.
Characters are mapped to a key via replacement.</p>
z_crypt.py (Python)
2006-07-03T05:43:24-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/496857-z_cryptpy/
<p style="color: grey">
Python
recipe 496857
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>The module show a way to simple data encryption.
Characters are mapped to a key via replacement.</p>