Re: Random NUmber Generator creates duplicates

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Dr John Stockton (spam_at_merlyn.demon.co.uk)
Date: 10/23/04


Date: Sat, 23 Oct 2004 23:16:27 +0100

JRS: In article <OG9tA4PuEHA.3788@TK2MSFTNGP09.phx.gbl>, dated Sat, 23
Oct 2004 08:44:26, seen in news:microsoft.public.scripting.vbscript, Bob
Barrows [MVP] <reb01501@NOyahoo.SPAMcom> posted :

>Why should a random number generator avoid duplicates? Random numbers are,
>well, random, not unique. "random" and "unique" are mutually exclusive.
>Think about coin tosses. If your idea that all possible events have to occur
>before an event was duplicated was true, then nobody would ever throw two
>heads in a row. Every time you pick from a list of possible outcomes, every
>item in that list has an equal chance of being picked, given that the choice
>algorithm has no "memory".
>
>The only way to guarantee uniqueness is to either:
>1. Store all possible outcomes and remove an outcome from the list when it's
>chosen until the list count reaches zero (or some arbitrary limit)
>2. Store all chosen outcomes and compare each new choice to the list before
>accepting it.

What we are discussing is, however, a pseudo-random number generator;
there are various types.

They will cycle through all accessible internal values, and then repeat
that sequence indefinitely.

In Borland Pascal and Delphi, for example, the generator has a 32-bit
seed and uses S' := (n*S + 1) mod 2^32 ; - the seed itself is
accessible, and cycles through all 2^32 possibilities in a nearly-
arbitrary looking order before repeating.

In VBS, using Randomize N, what is the proper range of N? 0<=N<1.0,
integer, dword, or other? And can the current seed be read back?

If the VB generator is of similar form, it may be possible to deduce its
algorithm and re-implement in VB, so that the seed is accessible; it
should be possible to implement the Pascal one (caution - choice of a
good N is non-trivial - see Knuth).

<URL:http://www.merlyn.demon.co.uk/pas-rand.htm> has more.
 

-- 
 © John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   MIME. ©
  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
  <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>   RAH Prins : c.l.p.b mFAQ;
  <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.


Relevant Pages

  • Re: random numbers in fortran
    ... extremely many possible outcomes. ... I make sure that I have a generator capabable of producing any of the ... I understand also that with a short cycle, ... Generally in practice then, could I not compensate for a short cycle by ...
    (comp.lang.fortran)
  • Re: random numbers in fortran
    ... extremely many possible outcomes. ... I make sure that I have a generator capabable of producing any of the ... I understand also that with a short cycle, ... The sample driver here prompts for N and 3 seeds. ...
    (comp.lang.fortran)
  • Re: random numbers in fortran
    ... a different set of outcomes becomes possible. ... could I not compensate for a short cycle by ... number returned after seeding and comparing subsequent returns to that value. ... this was better than the CBASIC RNG for CP/M which returned beautiful ...
    (comp.lang.fortran)
  • Re: random numbers in fortran
    ... a different set of outcomes becomes possible. ... could I not compensate for a short cycle by ... Sometimes the algorithm ... $.02 -Ron Shepard ...
    (comp.lang.fortran)
  • Re: some interview questions
    ... Given a function which produces a random integer in the range ... Use the 5-generator twice to give 25 possible outcomes. ... In the remaining 4 cases, use the generator again to get 20 outcomes. ... In the remaining case repeat step 1. ...
    (sci.math)