I am looking for a RANDOM function...
Author Message
I am looking for a RANDOM function...

I am looking for a function to generate random numbers
between two values.

Ex.

Min := -200000.00
Max :=  299999.99

Random(Min, Max)  =>  139529.27  (Output)

Do you have such a function in source code please let
me know or send it to me, if it is OK with you.

Best regards,

Klaus Kristensen
Denmark in Europe

Mon, 02 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

>>I am looking for a function to generate random numbers
>>between two values.

>>Ex.

>>Min := -200000.00
>>Max :=  299999.99

>>Random(Min, Max)  =>  139529.27  (Output)

>Dear Klaus
>Assuming that you have Pascal, it is quite easy to write a piece of code
>for this function and if you look in the HELP directory you will find it
>contained here.  If you wish it to be between two parameters then you
>can easily use a loop, so as to keep repeating untill it is between
>those numbers.
>If you still have problems please feel free to contact me.

I'm most likely completing someone's homework assignment here (shame!) but I
wanted to correct the impression that a loop may be required.  The code
should look something like this:

result := (max - min) * Random() + min;

Random() with no argument returns a real-type (now that I look at the docs
I'm not sure what this means, I'd assume double...anyone?) value 0 <= x < 1.
Call Randomize() before you call Random() the first time if you don't want
the sequence to repeat each time you run the program.

Hope this helped!

--
Terry Sikes               |  Software Developer

finger for PGP pub key    |  "Anyone programming in a 16-bit environment
My opinions - mine only!  |   isn't playing with a full DEC."

Tue, 03 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

]-I am looking for a function to generate random numbers
]-between two values.
]-
]-
]-Ex.
]-
]-Min := -200000.00
]-Max :=  299999.99
]-
]-Random(Min, Max)  =>  139529.27  (Output)
]-
]-
]-Do you have such a function in source code please let
]-me know or send it to me, if it is OK with you.

how 'bout this...

Function BoundedRandom(Min, Max : double) : double;
BEGIN   {==BoundedRandom==}
result := random*(Max - Min) + Min;
END;    {==BoundedRandom==}

only problem might be that the built-in "random" function
returns X | 0 <= X < 1, so while BoundedRandom can return
Min it cannot return Max.  (Of course if min and max can't
be exactly represented as a floating point number then
you'll have problems at the end points anyway...)

Mark Vaughan

]-
]-
]-
]-Best regards,
]-
]-Klaus Kristensen
]-Denmark in Europe

Tue, 03 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:
>I am looking for a function to generate random numbers
>between two values.

>Ex.

>Min := -200000.00
>Max :=  299999.99

>Random(Min, Max)  =>  139529.27  (Output)

Dear Klaus

Assuming that you have Pascal, it is quite easy to write a piece of code
for this function and if you look in the HELP directory you will find it
contained here.  If you wish it to be between two parameters then you
can easily use a loop, so as to keep repeating untill it is between
those numbers.
If you still have problems please feel free to contact me.

From Neil

--
Neil Jaques
01420 474290

Tue, 03 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

> ]-I am looking for a function to generate random numbers
> ]-between two values.
> how 'bout this...

> Function BoundedRandom(Min, Max : double) : double;
>   BEGIN   {==BoundedRandom==}
>     result := random*(Max - Min) + Min;
>   END;    {==BoundedRandom==}

> only problem might be that the built-in "random" function
> returns X | 0 <= X < 1, so while BoundedRandom can return
> Min it cannot return Max.  (Of course if min and max can't
> be exactly represented as a floating point number then
> you'll have problems at the end points anyway...)

> Mark Vaughan

try this, it will bound between the min and max region

function gen_random(min, max : integer) : integer;
begin
result := (max - min + 1) * Random + min;

end.

tanthalas

Tue, 03 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

>I am looking for a function to generate random numbers
>between two values.
>Ex.
>Min := -200000.00
>Max :=  299999.99
>Random(Min, Max)  =>  139529.27  (Output)
>Do you have such a function in source code please let
>me know or send it to me, if it is OK with you.
>Best regards,
>Klaus Kristensen
>Denmark in Europe

procedure TForm1.Button1Click(Sender: TObject);
var
r,x,y,z:real;
begin
r := random;            {create random number}
x := -200000.00 ;       {Lowest number}
Y := 499999.99  ;       {diff lowest to highest}
z := x + y*r ;          {calc value}
label1.caption :=
floattostrf(z,fffixed,7,2); {display value on label}
label2.caption :=
floattostrf(r,fffixed,7,2);  {display random on label}
end;

procedure TForm1.Button2Click(Sender: TObject);
VAR
x,y: real;
begin
if (Edit1.text <> '') and (Edit2.Text <> '' )
then begin

x := StrToFloat( Edit1.text);
y := StrToFloat(Edit2.text);

MakeRandom(X,Y);
Label3.Caption :=
floattostrf(MakeRandom(X,Y),fffixed,7,2);
end else
MessageDlg('Need some values', mtInformation,
[mbCancel], 0);

end;

function MakeRandom(X,Y:Real):real;
begin

result := x + y*random;    {calc value}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text := '';
Edit2.Text := '';
end;

end.

Wed, 04 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Sorry got late, forgot question;
should be;

function MakeRandom(rmin,rmax:Real):real;
begin
result := rmin + (rmax-rmin)*random;    {calc value}
end;

Wed, 04 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

: Ex.

: Min := -200000.00
: Max :=  299999.99

: Random(Min, Max)  =>  139529.27  (Output)

Use the Random() function.

Determine the *range* between the two numbers, use this information to
generate a random number the same size *as the range*.

Add in the value of the the "min" parameter. Presto!

eg:

size := max-min;
n    := Random(size);
n    := n+min;

--

Visit my home-page, get HTMLStrip and PMove:

Microsoft Network is prohibited from redistributing this work in any form,

Thu, 05 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

It is my experience that the compiler's 'random' function
is too correlated. If you need to use it many times
(define many your own way) you need to de-correlate it
by using the following:

const N=4; { the higher N the lower the correlation
(and also the speed). I usually use N=4. }

function MyRandom:real;
var i,r:integer;
begin
r:=random(N); { generates an integer in the range 0..N-1 }
for i:=0 to r do MyRandom:=random;
end;

Don't forget to 'randomize'.

Now, how to generate different distributions (e.g. Uniform,
Exponential, Normal etc.) is the subject for a whole seperate posting.

Shai.

Fri, 06 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

> It is my experience that the compiler's 'random' function
> is too correlated. If you need to use it many times
> (define many your own way) you need to de-correlate it
> by using the following:

Interesting... How would you show that? Is it just a "feeling" that
it's correlated, or do you have an example when this correlation
makes trouble for you?

--
Svante Granqvist        Speech, Music and Hearing
Phone +46-8-790 7561    Box 700 14
Fax   +46-8-790 7854    S-100 44 Stockholm

Fri, 06 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

> > It is my experience that the compiler's 'random' function
> > is too correlated. If you need to use it many times
> > (define many your own way) you need to de-correlate it
> > by using the following:
> > ...

> Interesting... How would you show that? Is it just a "feeling" that
> it's correlated, or do you have an example when this correlation
> makes trouble for you?

As I said, it is purely based on experience !
I once wrote a short simulation that generates random bits and inserts
a fixed pattern once every M bits (a frame). I then looked for the
probability of a random occurance of the fixed pattern elsewhere in
the frame. This probability is 'quite' easy to calculate (it depends on
the cyclic characteristics of that fixed pattern). Now, without the
decorrelation procedure I described in the previous posting I got odd
results from the simulation while decorrelating fit the calculations.

Shai.

Sat, 07 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

>> > It is my experience that the compiler's 'random' function
>> > is too correlated.

>> Interesting... How would you show that?

>As I said, it is purely based on experience !
>I once wrote a short simulation that generates random bits and inserts
>a fixed pattern once every M bits (a frame). I then looked for the
>probability of a random occurance of the fixed pattern elsewhere in
>the frame. This probability is 'quite' easy to calculate (it depends on
>the cyclic characteristics of that fixed pattern). Now, without the
>decorrelation procedure I described in the previous posting I got odd
>results from the simulation while decorrelating fit the calculations.

>Shai.

This just confirms one of the most important issues with pseudo-random
generators: Even the very best generators are predictable because after
all they ARE based upon calculations. This means that first you will have
to select a presumably good generator, and then you have to test it with
the actual application to verify that the generator works well also there.
Finally you should use some monitoring means to verify during the life of your
application that the results are reasonable (which can be really difficult).

The fact that the generator seems to fail in a particular application does
not neccessarily imply that it is bad, only that some unhappy correlation
might exist between that particular application and the generating formula.

Your experience is a bit surprising though: I failed to find any serial
correlation once I tried the usual tests on every second, third, fourth and
so on output from RANDOM. (The formula used is such that you wouldn't expect
any serial correlation).

regards Sven

Sun, 08 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

>>I am looking for a function to generate random numbers
>>between two values.

>>Ex.

>>Min := -200000.00
>>Max :=  299999.99

>>Random(Min, Max)  =>  139529.27  (Output)

use:

Random(1) * Range + LowerBound

so, for -5<x<5, use

Random(1) * 10 - 5

Matt

Wed, 11 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

> >>I am looking for a function to generate random numbers
> >>between two values.

> >>Ex.

> >>Min := -200000.00
> >>Max :=  299999.99

> >>Random(Min, Max)  =>  139529.27  (Output)

> use:

> Random(1) * Range + LowerBound

> so, for -5<x<5, use

> Random(1) * 10 - 5

> Matt

I think you meant Random here, not Random(1). Random(1) is
an _integer_ satisfying 0 <= Random(1) < 1 , which is really not all
that random. (If you don't believe it try this:

var j:integer;Oops:Bool;
begin

Oops:=True;

for j:=1 to 100 do
if Random(1)<>0 then Oops:=False;

if Oops then ShowMessage('Oops...');

end;

)

--
David Ullrich
Sig file accidentally deleted - sorry.

Sat, 14 Nov 1998 03:00:00 GMT
I am looking for a RANDOM function...

Quote:

>I am looking for a function to generate random numbers
>Klaus Kristensen
>Denmark in Europe

try this.  Sorry I got carried away playing with this.

unit Ranumber;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function MakeRandom(X,Y:Real):real;
var
Form1: TForm1;

implementation

{\$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
r,x,y,z:real;
begin
r := random;            {create random number}
x := -200000.00 ;       {Lowest number}
Y := 499999.99  ;       {diff lowest to highest}
z := x + y*r ;          {calc value}
label1.caption :=
floattostrf(z,fffixed,7,2); {display value on label}
label2.caption :=
floattostrf(r,fffixed,7,2);  {display random on label}
end;

procedure TForm1.Button2Click(Sender: TObject);
VAR
x,y: real;
begin
if (Edit1.text <> '') and (Edit2.Text <> '' )
then begin

x := StrToFloat( Edit1.text);
y := StrToFloat(Edit2.text);
MakeRandom(X,Y);
Label3.Caption :=
floattostrf(MakeRandom(X,Y),fffixed,7,2);
end else
MessageDlg('Need some values', mtInformation,
[mbCancel], 0);

end;

function MakeRandom(X,Y:Real):real;
begin
result := x + y*random;    {calc value}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text := '';
Edit2.Text := '';
end;

end.

Tue, 01 Dec 1998 03:00:00 GMT

 Page 1 of 2 [ 15 post ]

Relevant Pages