Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I am looking for a RANDOM function...

0 views
Skip to first unread message

Klaus Kristensen

unread,
May 16, 1996, 3:00:00 AM5/16/96
to

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.

Thanks in advance.


Best regards,

Klaus Kristensen
Denmark in Europe
<kla...@login.dknet.dk>

Terry Sikes

unread,
May 17, 1996, 3:00:00 AM5/17/96
to

Neil Jaques <ne...@jhome.demon.co.uk> wrote:
>In article <ZjscnisW...@login.dknet.dk>, Klaus Kristensen
><kla...@login.dknet.dk> writes

>>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
tsi...@netcom.com | C++ isn't a language, its an adventure!
finger for PGP pub key | "Anyone programming in a 16-bit environment
My opinions - mine only! | isn't playing with a full DEC."


Mark Vaughan

unread,
May 17, 1996, 3:00:00 AM5/17/96
to

In article <ZjscnisW...@login.dknet.dk>,
kla...@login.dknet.dk (Klaus Kristensen) wrote:
]-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


]-
]-Thanks in advance.
]-
]-
]-Best regards,
]-
]-Klaus Kristensen
]-Denmark in Europe
]-<kla...@login.dknet.dk>

Neil Jaques

unread,
May 17, 1996, 3:00:00 AM5/17/96
to

In article <ZjscnisW...@login.dknet.dk>, Klaus Kristensen
<kla...@login.dknet.dk> writes
>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
ne...@jhome.demon.co.uk
--
Neil Jaques
01420 474290

tant

unread,
May 17, 1996, 3:00:00 AM5/17/96
to

Mark Vaughan wrote:
>
> In article <ZjscnisW...@login.dknet.dk>,
> kla...@login.dknet.dk (Klaus Kristensen) wrote:
> ]-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

John De Beer / D. Mona Baumgartel

unread,
May 18, 1996, 3:00:00 AM5/18/96
to

kla...@login.dknet.dk (Klaus Kristensen) wrote:

>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.

>Thanks in advance.


>Best regards,

>Klaus Kristensen
>Denmark in Europe
><kla...@login.dknet.dk>


procedure TForm1.Button1Click(Sender: TObject);
{add button and 2 labels to your form}
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);
{ADD BUTTON2, LABEL3, AND 2 EDIT BOXES TO YOUR FORM}
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.


John De Beer / D. Mona Baumgartel

unread,
May 18, 1996, 3:00:00 AM5/18/96
to

Sorry got late, forgot question;
should be;

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


Larry Hadley

unread,
May 19, 1996, 3:00:00 AM5/19/96
to

Klaus Kristensen (kla...@login.dknet.dk) wrote:
: 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;

--
-- DLH "Warhammer" lha...@knet.flemingc.on.ca
Visit my home-page, get HTMLStrip and PMove:
http://www.knet.flemingc.on.ca/~lhadley/Profile.html

Microsoft Network is prohibited from redistributing this work in any form,
in whole or in part. Copyright, Larry Hadley, 1996. Please send notices of
violation to lha...@knet.flemingc.on.ca and Postm...@microsoft.com


Shai Benjamin

unread,
May 20, 1996, 3:00:00 AM5/20/96
to

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.

Svante Granqvist

unread,
May 20, 1996, 3:00:00 AM5/20/96
to Shai Benjamin

Shai Benjamin wrote:
>
> 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
sva...@speech.kth.se http://www.speech.kth.se/~svante

Shai Benjamin

unread,
May 21, 1996, 3:00:00 AM5/21/96
to sva...@speech.kth.se

Svante Granqvist wrote:
> Shai Benjamin wrote:
> >
> > 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.

Matthew B. Comer

unread,
May 25, 1996, 3:00:00 AM5/25/96
to

>In article <ZjscnisW...@login.dknet.dk>, Klaus Kristensen
><kla...@login.dknet.dk> writes
>>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

David Ullrich

unread,
May 28, 1996, 3:00:00 AM5/28/96
to

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.

John De Beer / D. Mona Baumgartel

unread,
Jun 14, 1996, 3:00:00 AM6/14/96
to

kla...@login.dknet.dk (Klaus Kristensen) wrote:

>I am looking for a function to generate random numbers

>Klaus Kristensen
>Denmark in Europe
><kla...@login.dknet.dk>

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}

end;

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

0 new messages