Pseudo-Random Number
Generators (PRNGs)
Site Map Feedback

Up Rand Random ShortRand Twister

Think of a Number

The algorithms in this section (in the gold menu, above) generate Pseudo-Random Numbers.
Each offer different features to the standard rand() which goes something like this:

  unsigned long LastRand;
  return ((LastRand = LastRand * 214013L + 2531011L) >> 16) & 0x7FFF;

Seed

'Seeding' is the initialisation of LastRand. If LastRand is set to zero (for example) the next call to rand will always be 38, and the next value 7719. You can test this using the following code:
  srand(0);
  ASSERT(rand()==38);
  ASSERT(rand()==7719);
  srand(0);
  ASSERT(rand()==38);
  ASSERT(rand()==7719);

The Pseudo part has become very important in a couple of applications. Sometimes it is valuable to know that the same sequence will happen if you start from a specific seed (when you specifically don't want this behaviour, always seed using something fairly random like the value returned from GetTickCount();). If you want, for example, to give many users the same 'random' daily quote (QOTD) from a quotations database: use the date (withouth the time part) to seed the PRNG. Each user will see the same quote on the same day and if a 'next' facility is available, they all see the same sequence and can tell their friends to look at today's fifth quote knowing that everyone will see the same one.

The most important application, though, doesn't use seed and is created by smoothing the randomness: moving from something like white noise:
White Noise to coherent noise: Coherent Noise which generates repeatable, natural looking Procedural Textures.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.