Re: When random isn't random

From: Jens Gruschel (nospam_at_pegtop.net)
Date: 12/13/03


Date: Sat, 13 Dec 2003 11:07:35 +0100


> >Description: Machines are getting so fast, I wonder if it is time to
> >slow down the Random function by making it use 64-bit seeds and results.
> >Maybe call it Random64( or 63 or 62 if we need to waste a couple of
> >bits)?
>
> IMHO, the Random and Randomize functions should not be changed, for
> compatibility reasons.

That's why John wants to introduce the new function with a new name.

> A new Unit should instead be provided, containing thoughtfully- named
> and -written routines including versions of Random, Deal, and Shuffle
> (and, if there is not one already, a Sorting Unit). The task could be
> subcontracted to skilled volunteer labour, such as is found here.

A new unit for randomizing might be useful. A unit for sorting? What do you
want to sort? TList has a Sort method. And shuffling is nothing but a
special case of sorting. Try it with a TList and in the compare function
return -1 or 1 at random.

> If a reasonably efficient reverse algorithm is known, it should be
> provided; it is necessarily an alternative, and reversibility might be
> of use.

There is. For example x' = (x * 1664525 + 1013904223) mod $100000000 can be
undone by x = (4276115635 * (x' - 1013904223)) mod $100000000 (that's the
numbers I use in my own unit - see below)

> The new routines should not use a specific RandSeed; instead, each
> should have a first parameter var seed : <type> .

I solved this by writing a random class which holds its own RandSeed.
Normally one instance is enogh, that's why one instance is created
automatically. But if you ever need more random generators (maybe because
you need to reverse RandSeed somewhere - or because you want the same
sequence each time, initializing with a certain RandSeed, but don't want to
influence other random generators), you are free to create as many instances
as you need.

I can send you my unit if you wish (). It uses another algorithm as Random
does, but it's quite easy to replace it with the original Delphi behaviour
(maybe I could do this before I send it to you). Unfortunatelly it only uses
32 bit (like Random), maybe I should work on it again and introduce 64 bit
numbers.

Jens

P.S. Here are the public methods of my random class:

    constructor Create; overload;
    constructor Create(StartSeed: Integer); overload;
    procedure Randomize(Mode: TRandomizeMode = prmDefault);
    function Next(Limit: Integer): Integer; overload;
    function Next(Min, Max: Integer): Integer; overload;
    function Next: Double; overload;
    function NextGaussian(Mean: Double = 0.0; StdDev: Double = 1.0): Double;
    function Current(Limit: Integer): Integer; overload;
    function Current(Min, Max: Integer): Integer; overload;
    function Current: Double; overload;
    function CurrentGaussian(Mean: Double = 0.0; StdDev: Double = 1.0):
Double;
    function Previous(Limit: Integer): Integer; overload;
    function Previous(Min, Max: Integer): Integer; overload;
    function Previous: Double; overload;
    function PreviosGaussian(Mean: Double = 0.0; StdDev: Double = 1.0):
Double;
    property Seed: Integer read FRandSeed write FRandSeed;



Relevant Pages

  • Re: Is it possible to define two separate "Sort" methods in the same class?
    ... (for sorting mBuyLimits), and the other one is for sorting by "Price" ... in ascending order (for sorting mSellLimits)? ... I want to use IComparable and define the built-in Sort method. ...
    (microsoft.public.dotnet.general)
  • Re: Sorting with datasource ArrayList
    ... The Sort method of the Gridview control raises the Sorting and Sorted ... should sort your data source according to the ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Problem sorting a single column in a shared workbook
    ... I´m using the sort method to do a simple sorting in a column. ... vba sort method or from menue) ALL columns on that sheet are sorted, ... not only the selected column. ...
    (microsoft.public.excel.programming)
  • DataGridView.Sort
    ... DataGridView to bind a dataset. ... (DataGridViewColumn, ListSortDirection) ... Event the sort method works fine (for default sorting), ...
    (microsoft.public.dotnet.framework.windowsforms.controls)