IBM Support

Generating different sequences of uniformly distributed random numbers using OPL script for every run

Question & Answer


Question

How can I generate different sequences of uniformly distributed random numbers by using a different random seed for every run?

Answer

OPL script provides the user with a couple of functions to manage the generation of uniformly distributed random numbers:

1) srand(n)- Used to initialize the generator (seed) for the pseudo random numbers.

2) rand(m)- Returns a pseudo random integer modulo m (thus the number will be between 0 and m-1, both inclusive).

If you would like to generate different random numbers every time you ran your OPL model, then you will need to use the srand function to provide a different random seed for each run. Time could serve as one such seed and hence the subsequent rand function calls would generate different random numbers in every run. One such implementation using OPL scripts could look like the following:


execute{
  var now = new Date();
    Opl.srand(now.getTime()%Math.pow(2,31));
}
int myRange = 60;
int randomArray[i in 1..20] = 1 + rand(myRange);
execute{
writeln("randomArray="+randomArray);
}
 

[{"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Area:ILOG Script for OPL\/OPL Script","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"12.2;12.3;12.4;12.5;12.5.0.1;6.0;6.0.1;6.1;6.1.1;6.2;6.3","Edition":"All Editions","Line of Business":{"code":"LOB10","label":"Data and AI"}},{"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Area:ILOG Script for OPL\/OPL Script","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"12.5.0.1;12.5;12.4;12.3;12.2.0.1;12.2","Edition":"All Editions","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
16 June 2018

UID

swg21595936