Lazarus

Programming => General => Topic started by: coradi on February 06, 2018, 08:17:57 pm

Title: Random with Float Values?
Post by: coradi on February 06, 2018, 08:17:57 pm
Hallo,
how can I generate random float values? and then calculate with that.
Title: Re: Random with Float Values?
Post by: Blaazen on February 06, 2018, 08:27:29 pm
function Random; returns numbers in interval 0..1.

You have to call Randomize; once in the begining of your program.
Title: Re: Random with Float Values?
Post by: Handoko on February 06, 2018, 08:46:10 pm
I should add, the result is never = 1.

Code: Pascal  [Select][+][-]
  1. var
  2.   R: Real;
  3.   I: Integer;
  4. begin
  5.   R := Random * 10;
  6.   I := Random(5);
  7. end;
  8.  

The results are:

https://www.freepascal.org/docs-html/rtl/system/random.html
Title: Re: Random with Float Values?
Post by: coradi on February 07, 2018, 07:23:58 am
ahhh, great :-)
and how can I get values like 23,34?
I tried
 Label1.Caption := floattostr (R:2:2);

and why is 2.5=2 and not 3?
  Writeln (Round(2.5));      { Prints 2 (down) }
  Writeln (Round(3.5));      { Prints 4 (up)   }

I doesn't know, that Random generate Real values too :-)
I don't need -3 or something at this moment :-)
Great :-)

Thanks
Title: Re: Random with Float Values?
Post by: Handoko on February 07, 2018, 08:33:12 am
and how can I get values like 23,34?

Try this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Value: Real;
  4.   Fmt: TFormatSettings;
  5.   S: string;
  6. begin
  7.  
  8.   Value := Random*20;
  9.   Fmt := FormatSettings;
  10.   Fmt.DecimalSeparator := ',';
  11.  
  12.   // Using FloatToStrF
  13.   S := FloatToStrF(Value, ffFixed, 0, 2, Fmt);
  14.   ShowMessage(S);
  15.  
  16.   // Using FormatFloat
  17.   S := FormatFloat('0.00', Value, Fmt);
  18.   ShowMessage(S);
  19.  
  20. end;

and why is 2.5=2 and not 3?
  Writeln (Round(2.5));      { Prints 2 (down) }
  Writeln (Round(3.5));      { Prints 4 (up)   }

There are lots of known rounding algorithms, Pascal's Round function uses Banker's Rounding. It is mentioned on the documentation:

https://www.freepascal.org/docs-html/rtl/system/round.html
https://en.wikipedia.org/wiki/Rounding

If you need round up if exactly in middle, read here:
https://forum.lazarus.freepascal.org/index.php/topic,36409.0.html
Title: Re: Random with Float Values?
Post by: coradi on February 07, 2018, 09:03:19 am
oh, okay..a much more to write :2:2 were so much easier  ;D
Title: Re: Random with Float Values?
Post by: wp on February 07, 2018, 09:43:43 am
Code: Text  [Select][+][-]
  1.   Str(Value:0:2, s);
  2.   ShowMessage(s);
But you don't get the decimal comma this way.
Title: Re: Random with Float Values?
Post by: Handoko on February 07, 2018, 10:34:32 am
You can, just need an extra line:

Code: Pascal  [Select][+][-]
  1.   Str(Value:0:2, S);
  2.   S := S.Replace('.', ',');
  3.   ShowMessage(S);
Title: Re: Random with Float Values?
Post by: wp on February 07, 2018, 12:17:31 pm
I know. But he's just too lazy for typing.
Title: Re: Random with Float Values?
Post by: coradi on February 13, 2018, 12:38:42 pm
I have found
FloattoCurr
But I don't understand to use it!?
HAve anyone a example? Only one line, ist enough :-)
Habe have 5 Books für Delphi, and the LAzarus Referenz....bot nowhere is an Example or something..mostly FloattoCurr doesn't exists in this books :-(
Title: Re: Random with Float Values?
Post by: howardpc on February 13, 2018, 01:02:32 pm
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$Mode objfpc}{$H+}
  4.  
  5. uses sysutils;
  6.  
  7. var
  8.   extAmount: Extended;
  9.   currAmount: Currency;
  10.  
  11. begin
  12.   extAmount := 100.00 / 3;
  13.   currAmount := FloattoCurr(extAmount);
  14.   WriteLn('value of (100.00/3) as Extended = ',extAmount:10:8);
  15.   WriteLn('value of (100.00/3) as Currency = ',currAmount:10:8);
  16.   ReadLn;
  17. end.
Title: Re: Random with Float Values?
Post by: coradi on February 13, 2018, 01:19:48 pm
ah, ok thanks..now i understand :-)
The is a Viriable Type from currency :-)

VAR S: Currency;
  S := FloattoCurr(value); 
ShowMessage(S);

Thanks :-)
Title: Re: Random with Float Values?
Post by: wp on February 13, 2018, 01:23:35 pm
Why are you dealing with currency now? The initial question was about random numbers. Then rounding. Now currency.

Please state clearly what your question is and you will get competent help. Otherwise this is just nonsense.
Title: Re: Random with Float Values?
Post by: Handoko on February 13, 2018, 06:52:21 pm
I guess the TS was looking for a very simple command like:

S := Something(R:2:2);

But so far as I know, there no such simple function in Pascal that can convert real to string and conform with the default decimal symbol.
Title: Re: Random with Float Values?
Post by: molly on February 13, 2018, 07:00:55 pm
But so far as I know, there no such simple function in Pascal that can convert real to string and conform with the default decimal symbol.
You mean like this ?
Code: Pascal  [Select][+][-]
  1.   WriteStr(S, R:2:2);
Or did you meant default (system configured) decimal point ?
Title: Re: Random with Float Values?
Post by: Handoko on February 13, 2018, 07:13:17 pm
and how can I get values like 23,34?
I tried
 Label1.Caption := floattostr (R:2:2);
Title: Re: Random with Float Values?
Post by: molly on February 13, 2018, 07:26:25 pm
Ah, thanks Handoko as my glance was too quick  :-[
Title: Re: Random with Float Values?
Post by: Thaddy on February 13, 2018, 08:22:21 pm
I have a super- routine for that:
Code: Pascal  [Select][+][-]
  1. function dwim:dwim;
  2. begin
  3.  dwim :=dwim;
  4. end;
Title: Re: Random with Float Values?
Post by: Thaddy on February 14, 2018, 09:47:46 am
I guess the TS was looking for a very simple command like:

S := Something(R:2:2);

But so far as I know, there no such simple function in Pascal that can convert real to string and conform with the default decimal symbol.
Include sysutils, use the typehelpers.
Code: Pascal  [Select][+][-]
  1.     Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  2.     Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  3.     Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  4.     Function ToString: string; overload; inline;
Easier that that set is hardly possible in any language!
TinyPortal © 2005-2018