Search code examples
inno-setuppascalscript

Execute program with random number as a parameter in Inno Setup?


How can I include an random number in a string in Inno Setup?

[Run]
#define rndH Random(24) 
#define rndM Random(60)

Filename: "schtasks"; \
  Parameters: "/Create /F /SC DAILY /ST {rndH}:{rndM} /RU SYSTEM /RL HIGHEST /TN ""Program Title"" /TR ""'{app}\program.exe'""";

When I try the code above, I get an error

[ISPP] Undeclared identifier: "Random"

Thanks


Solution

  • The code in your answer works, but it is unnecessarily complicated:

    • Use scripted constant parameter, to avoid creating two functions that differ just by a constant;
    • Use Format function to pad the number to two digits;
    • No need to use global variables (though my version does not need any variables at all actually).
    [Run]
    Filename: "schtasks"; \
      Parameters: "/Create /F /SC DAILY /ST {code:MyRand|24}:{code:MyRand|60} ...";
    
    [Code]
    
    function MyRand(Param: string): string;
    begin
      Result := Format('%.2d', [Random(StrToInt(Param))]);
    end;