Recent

Author Topic: randomrange  (Read 254 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 380
randomrange
« on: December 06, 2025, 10:07:36 am »
Is that really useful for small ranges ?
I can tell there are 1000+  just 1 before and 1000+  just 2 after (see image)

randomize does no change anything at all.
Code: Pascal  [Select][+][-]
  1.   for ...
  2.   begin
  3.  
  4.     randomize();
  5.     random12 := RandomRange(1,3);
  6.     SynEdit1.Lines.Add(IntToStr(random12 ));
  7.  
  8.   end;
  9.  


Any suggestion ?
« Last Edit: December 06, 2025, 10:20:01 am by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 18712
  • To Europe: simply sell USA bonds: dollar collapses
Re: randomrange
« Reply #1 on: December 06, 2025, 10:21:24 am »
I don't see anything wrong.
A random in the range 1..3 is returned. 3 is not part of the range... see:
https://www.freepascal.org/docs-html/rtl/math/randomrange.html

So the function can only return 1 or 2 and it does.
Code: Pascal  [Select][+][-]
  1. //tested with
  2. {$ifdef fpc}{$mode objfpc}{$endif}
  3. uses math;
  4. type range = 0..999;
  5. var
  6.  i:integer;
  7. begin
  8.   randomize;
  9.   for i in range do writeln(i:5,randomrange(1,3):2);
  10. end.
If 3 needs to be included, make the upper range 4.
« Last Edit: December 06, 2025, 10:30:27 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 380
Re: randomrange
« Reply #2 on: December 06, 2025, 10:36:19 am »
Ahh, forget that, works fine. Had SynEdit1.Lines.Add(IntToStr(random12 )) inside wrong loop.
Thanks.
lazarus 3.2-fpc-3.2.2-win32/win64

 

TinyPortal © 2005-2018