#1 2016-12-03 23:30

TGB
Member
Registered: 2016-06-12
Posts: 14

"Random" function not supported?

I have several files from my camera which, according to Windows, were changed at the same time including seconds.

I though I could add some random number to the file name. I used the script:

var
DateTime: TDateTime;

begin
Randomize;
DateTime := FileTimeModified(FilePath);
FileName := 'DCIM_'
+  FormatDateTime('yyyymmdd', DateTime)
+ '_'
+  FormatDateTime('hhnnss', DateTime)
+ '_'
+ IntToStr(Random(10))
+    WideExtractFileExt(FileName);
end.
          

I got the following error: Unknown identifier 'Random'.
Why is "Random" not supported? It is the basic math function.

Offline

#2 2016-12-03 23:47

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,367

Re: "Random" function not supported?

Why is "Random" not supported?

There is a whole set of random generator functions:

function RandomRange(const AFrom, ATo: Integer): Integer;
function RandomFloat: Extended;
function RandomBoolean: Boolean;

The Random function was never added because a superior function RandomRange can easily do its job.

By the way, you shouldn't call Randomize more than once per renaming cycle, as stated in the documentation. If you do, its effect will be greatly diminished.

To call it only once you can use a dummy initialization variable, like so:

var
  Initialized: Boolean;
begin
  if not Initialized then
  begin
    Randomize;
    Initialized := True;
  end;
end.

Also, you can use a new built-in Randomize rule which is available since v6.6.0.3 Beta. It was part of the Serialize rule in prior versions.

Offline

#3 2016-12-18 15:00

TGB
Member
Registered: 2016-06-12
Posts: 14

Re: "Random" function not supported?

Thanks!

Offline

Board footer

Powered by FluxBB