THIS IS NOT YET AN OFFICIAL MOCHIKIT COMPONENT
MochiKit.Random - Random numbers and algorithms
var randomData = map(randRange, repeat(2000, 1000)); // -> Array of 1000 elements with random numbers from 0 to 1999
Random numbers and associated algorithms. The API is largely a subset of the Python random [1] module. By default uses the Mersenne Twister [2] generator, but other generators, including the native Math.random, can be used. See source code for details.
Return the next random floating point number in the range [0.0, 1.0).
Initialize the basic random number generator. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported.
Return an object capturing the current internal state of the generator. This object can be passed to setState() to restore the state.
state should have been obtained from a previous call to getState(), and setState() restores the internal state of the generator to what it was at the time setState() was called.
randRange([start=0], stop[, step=1]):
Choose a random item from range(start, stop[, step]) Half-open range, i.e does Not include stop
Return a random element from the non-empty sequence seq.
Shuffles an array using the Fisher-Yates algorithm [3] (Knuth). O(N) (in-place algorithm)
Returns the shuffled input array to enable chaining.
(..shuffling an array by sorting using a random comparator is a Bad idea [4])
Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.
Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).
Note: does not support iterator as input, use list(iterable).
Generates a unique random range of numbers from 0..N-1 (or rather f(0)..f(N-1) ) (no number occurs twice. think dealing a deck of cards)
todo: might drop this. equivalent to:
deal = shuffle(map(func || operator.identity, range(numItems)));
Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a. The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().
[1] | Python random module: http://docs.python.org/library/random.html |
[2] | Mersenne Twister 19937: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
[3] | Fisher-Yates shuffle: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle |
[4] | Microsoft does shuffling: http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html |
Copyright 2005-2010 Bob Ippolito <bob@redivi.com>. This program is dual-licensed free software; you can redistribute it and/or modify it under the terms of the MIT License or the Academic Free License v2.1.