What is the algorithm to have random reals using x++ in Dynamics AX?
            Asked
            
        
        
            Active
            
        
            Viewed 2,985 times
        
    2 Answers
8
            The old way (and only if using Axapta 3.0) is to use the Random class which is listed in the AOT under System Documentation\Classes. It will return a 15 bit integer only. See AX Daily.
But like Alex, I will prefer using the newer xGlobal::randomPositiveInt32().
dice = (xGlobal::randomPositiveInt32() mod 6) + 1;
        Jan B. Kjeldsen
        
- 17,817
 - 5
 - 32
 - 50
 
- 
                    is there a method to generate random negative numbers? – Tassisto Dec 04 '11 at 11:54
 - 
                    2Sure: -xGlobal::randomPositiveInt32() Did you see the minus? – Jan B. Kjeldsen Dec 05 '11 at 11:22
 - 
                    1Or: (xGlobal::randomPositiveInt32() mod 7) - 3, to generate -3 to +3. – Jan B. Kjeldsen Dec 05 '11 at 11:24
 
3
            
            
        You can easily generate a positive int with this method, then just turn it into a real and divide after if you want decimals.
i = xGlobal::randomPositiveInt32();
        Alex Kwitny
        
- 11,211
 - 2
 - 49
 - 71