Tuesday, January 26, 2010

Generating a random number

It is not so obvious thing - generating random numbers in AX. I found 3 different ways to do that:
  1. Kernel class Random (not visible in AOT). Generates a value in the range [0..32767] by calling nextInt() instance method.
  2. Class RandomGenerate (which extends Random class). Generates a value in the specified range by calling randomInt() instance method.
  3. xGlobal::randomPositiveInt32() method. Generates a value in the range [0..2147483647].

Actually, the third way seems to be the most preferable because there is some trick with first two ones. If the new Random class instance will be created each time before calling nextInt(), the output values will be very far from random. For example, the following code:
static void testRandom(Args _args)
{
    Random random;
    int i;

    for (i = 0; i < 3; i++)
    {
        random = new Random();
        info(int2str(random.nextInt()));
    }
}

On the first run will give the following output:
30800
30800
30800
And on the second run:
30810
30810
30810

21 comments:

  1. As a fourth option you could use System.Random.
    You can specify a seed in the new to get better results. You can also get random Double or Byte values.

    ReplyDelete
  2. Daily useful things. Great idea. Will follow it with interest.

    /Jonas

    ReplyDelete
  3. 2 Chrono:
    I would recommend to avoid using System.Random.
    It will add a rather large overhead compared to the use of X++ functions.

    ReplyDelete
  4. It is indeed slower. However, for it to be actually noticeable you would have to be generating thousands and thousands of random numbers. :p

    Thanks for letting me know though.

    ReplyDelete
  5. I just tried method #2 to generate 10,000 unique numbers using a range of 1,000,000-9,999,999.

    It performed about 12,000 operations to get 10,000 "unique" numbers. They were all FAR from unique however.

    ReplyDelete
  6. **I meant to say, far from random. They looked almost sequential.

    ReplyDelete
  7. The 3rd option for me was the best way to generate a large group of random numbers. The first two just ends up being sequential. I wrote a little blog post inspired by this:

    http://alexondax.blogspot.com/2011/04/generating-better-random-numbers-with.html

    ReplyDelete
  8. Well, you are right about odd behavior of the option #2 with ranges that are outside [0, 32767] range. The implementation of the randomInt() method is bad.

    To get random numbers in the range I suggest the following algo:

    n = xGlobal::randomPositiveInt32() mod (9999999 - 1000000) + 1000000;

    ReplyDelete
  9. Hello, its nice article on the topic of media print, we all be familiar
    with media is a fantastic source of facts.

    Visit my weblog; airplanes games

    ReplyDelete
  10. have a peek at this website see here linked here navigate here read here his response

    ReplyDelete
  11. More than a great article, but you can learn about a package of distinguished services in buying and selling used, damaged and old cars in the Kingdom of Saudi Arabia through Selling cars in Saudi Arabia We save you a lot of time and effort in selling your car Car buying company in Saudi Arabia So you can get enough information about our distinguished services in Used car buying and selling company Which undoubtedly gives you many of the most wonderful advantages in the field of buying and selling cars Buying all kinds of cars in Saudi Arabia

    ReplyDelete