Planning Analytics

 View Only
  • 1.  Cognos TM1 Rand() function – it is NOT quite random

    Posted Thu June 01, 2023 05:44 AM

    Hi, all

    Just came across this. I tested in our PA server and it is still the case. Just wondering the reason behind this?

    https://everanalytics.wordpress.com/2015/07/05/cognos-tm1-rand-function-it-is-not-quite-random-after-all/

    IBM Cognos TM1 Rand () function can generate up to 65,536 unique numbers.  Thereafter it starts repeating the numbers.  To test this behavior, enter below piece of code in the prolog of a TI process and execute it.

    i = 1;
    iMax = 65536;
    WHILE ( i <= iMax *2 );
        IF (i <= iMax);
            vFile = 'sk1.txt';
        ELSE;
            vFile = 'sk2.txt';
        ENDIF;
        vFile = GetProcessErrorFileDirectory | vFile ;
        AsciiOutput (vFile, NumberToString (Rand()) );
        i = i + 1;
    END;



    ------------------------------
    mvp morgan
    ------------------------------


  • 2.  RE: Cognos TM1 Rand() function – it is NOT quite random

    IBM Champion
    Posted Fri June 02, 2023 02:19 AM

    Hi Morgan,

    It is impossible for any computer to generate truly random numbers, as nothing happens randomly when it comes to code. Random numbers are actually generated by a equation, based on a "seed" which are often time based. At some point the equation will start over, that will always happen. In this case, it seems to be at 65,536 :-)



    ------------------------------
    Emil Malmberg Fosdal
    Solution Architect
    CogniTech A/S
    ------------------------------



  • 3.  RE: Cognos TM1 Rand() function – it is NOT quite random

    Posted Fri June 02, 2023 03:59 AM
    Edited by Renaud MARTIAL Fri June 02, 2023 04:00 AM

    Hello,

    I might add that usually we do not really need random numbers, but rather a way to generate unique identifiers to create temporary objects in TI processes (well, it was before PA capabilities to build really temporary views and subsets).
    The classic way is to build a string with some contextual information, a timestamp and a random number, which is often quite sufficient for this purpose.

    In our case, however, it seemed to be insufficient, as we have a tremendous number of processes that are executed simultaneously, and some time ago it appeared twice that this method generated the same string, which resulted in a lock in the TM1Server.

    So the point should be to have the capability to get an ID which is guaranteed to be truly unique, similar to a UUID.
    Fortunately, there's a way to achieve this with the availability of the RunProcess function, which returns a JobID.
    Since it's built by the PA server itself - well, the part that handles RestAPI calls - and can be used to uniquely identify a TI Process call, it's by design quite unique.

    So a nice way to achieve this UniqueID retrieval is to use RunProcess to execute a process that does nothing, and use its return value.

    Regards,



    ------------------------------
    Renaud MARTIAL
    PA Applications Manager / Tech lead
    L'Oréal
    ------------------------------



  • 4.  RE: Cognos TM1 Rand() function – it is NOT quite random

    IBM Champion
    Posted Fri June 02, 2023 04:40 AM

    Hi Renaud,

    I have had the same issue as well. 

    If you have a counter, a way to handle can also be to append the RAND() number every 65k records. If not, appending multiple RAND()'s can also be a solution, as they will have multiple seeds and limit the chance of overlapping numbers.



    ------------------------------
    Emil Malmberg Fosdal
    Solution Architect
    CogniTech A/S
    ------------------------------



  • 5.  RE: Cognos TM1 Rand() function – it is NOT quite random

    IBM Champion
    Posted Fri June 02, 2023 04:54 AM

    Maybe another approach to deriving a unique reference is to use the undocumented MilliTime function.

    This will return a numeric value like 1813725061

    If you want to be really sure of uniqueness, you could go with something like the below:

    sView=GetProcessName | Str(MilliTime() * Rand(), 10, 0 );

    MilliTime is supported and have it on good authority that it is not going anywhere. In PAW, you would just need to ignore the error PAW returns as PAW is not aware of this function. Your process will however save and execute.



    ------------------------------
    George Tonkin
    Business Partner
    MCI Consultants
    Johannesburg
    ------------------------------



  • 6.  RE: Cognos TM1 Rand() function – it is NOT quite random

    Posted Fri June 02, 2023 05:35 AM

    Yes, I use this undocumented function as well append with a timestamp. I really hope TM1 v12 can have some sort of UUID function but unfortunately, I don't think it will be included this time. :(



    ------------------------------
    mvp morgan
    ------------------------------



  • 7.  RE: Cognos TM1 Rand() function – it is NOT quite random

    Posted Fri June 02, 2023 05:33 AM

    Good trick. I know RUNPROCESS can return a unique iD but never realised that I can use it for uniqueness. 



    ------------------------------
    mvp morgan
    ------------------------------



  • 8.  RE: Cognos TM1 Rand() function – it is NOT quite random

    Posted Fri June 02, 2023 05:32 AM

    Would it be even better to let user specify a seed into this function?  :)



    ------------------------------
    mvp morgan
    ------------------------------