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>
>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."
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>
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
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
>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.
function MakeRandom(rmin,rmax:Real):real;
begin
result := rmin + (rmax-rmin)*random; {calc value}
end;
: 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
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.
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
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.
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.
>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