Recent

Author Topic: Random with Float Values?  (Read 6802 times)

coradi

  • Full Member
  • ***
  • Posts: 148
Random with Float Values?
« on: February 06, 2018, 08:17:57 pm »
Hallo,
how can I generate random float values? and then calculate with that.
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Random with Float Values?
« Reply #1 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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Random with Float Values?
« Reply #2 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:
  • 0 <= R < 10
  • I = 0, I = 1, I = 2, I = 3, I = 4

https://www.freepascal.org/docs-html/rtl/system/random.html

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Random with Float Values?
« Reply #3 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
« Last Edit: February 07, 2018, 07:33:51 am by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Random with Float Values?
« Reply #4 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
« Last Edit: February 07, 2018, 08:42:22 am by Handoko »

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Random with Float Values?
« Reply #5 on: February 07, 2018, 09:03:19 am »
oh, okay..a much more to write :2:2 were so much easier  ;D
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Random with Float Values?
« Reply #6 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.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Random with Float Values?
« Reply #7 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);

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Random with Float Values?
« Reply #8 on: February 07, 2018, 12:17:31 pm »
I know. But he's just too lazy for typing.

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Random with Float Values?
« Reply #9 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 :-(
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Random with Float Values?
« Reply #10 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.

coradi

  • Full Member
  • ***
  • Posts: 148
Re: Random with Float Values?
« Reply #11 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 :-)
« Last Edit: February 13, 2018, 01:22:57 pm by coradi »
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Random with Float Values?
« Reply #12 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.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Random with Float Values?
« Reply #13 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.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Random with Float Values?
« Reply #14 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 ?

 

TinyPortal © 2005-2018