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. نقدم لكم الشركة الرائده فى عالم تنظيف خزانات وتسليك المجارى بأحدث اجهزة الكشف وبدون تكسير بالاضافة الى كشف تسربات المياه بالرياض بالاعتماد على اكفأ المهندسين المختصين بأعمال السباكة المنزلية واصلاح المواسير الداخليه والخارجية واللذين يؤدون خدمتهم على اكمل وجه وبأقل التكاليف واجود النتائج فاتصلو بنا عملاؤنا الكرام من اى مكان بالسعودية لنتشرف بخدمتكم على الرحب والسعه مع تقديم خدمات شراء اثاث مستعمل بالرياض بافضل الاسعار خدمات شركة تنظيف خزانات بالرياض لدينا هي الاكفأ علي الاطلاق
    ان شركتنا افضل شركة شركة كشف تسربات المياه بالرياض بالرياض هى الحل الذى امامك فى حالة وجود تسربات لديك بالجدران والاسقف والارضيات حيث نعد من اكبر الشركات المتخصصه فى هذا المجال حيث نقوم باستخدام احدث التقنيات المستخدمة فى الكشف الدقيق عن التسربات من خلال اكفأ الكوادر البشرية والمهندسين المختصين بذلك وبأرخص التكاليف , فاتصل بنا لتتخلص نهائيا من تسربات المياه لديك

    ReplyDelete