web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Random Number Generation

(0) ShareShare
ReportReport
Posted on by

Hello,

 

We are implementing one functionality where we need to generate random number between 5 digits to 9 digits.

First I tried with 

//Job 1

Random Random = new Random();
int myrandomnumber;
;
myrandomnumber= Random.nextInt() ;

In above job it is generating random number of 5 digits and it seems everytime number starts with 30***. We tried to generate two random numbers and linked them.

Suppose we need to generate 7 digits number then 5 digit by above job and 2 digits by myrandomnumber= Random.nextInt() mod 100 ; and then linked them but it generates 1 digit number also. So we can not rely on this functions.

Then I tried to generate random number using 

//Job 2

RandomGenerate randomGenerate;
randomGenerate = RandomGenerate::construct();
randomGenerate.parmSeed(new Random().nextInt());

switch(numberOfDigits)
{

case 5:
RandomNo = randomGenerate.randomInt(10001,99999);
break;

case 9:
RandomNo = randomGenerate.randomInt(100000001,999999999);
break;
}

This is good option but in this while we are generating numbers of 9 digits mostly they are starting from 999/998/997. I never get number starting from 1 to 8. Can anyone explain why this is happening? Is there any alternate approach we can use to generate random numbers those are actually random?(any number from entire range)

Thanks in advance!

Regards,

Smit

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mea_ Profile Picture
    60,286 on at

    Hi Smit Patel,

    If I were you, I would use System.Random, did you try it ?

  • Verified answer
    Martin Dráb Profile Picture
    239,399 Most Valuable Professional on at

    The logic responsible for the behavior is in RandomGenerate.randomInt(). I agree that it seems to work incorrectly, so simply don't use it (and you can report it as a bug to Microsoft).

    You have several workarounds.

    • Take X++ class Random and covert nextInt() to the required range by yourself. That's exactly what randomInt() tries to do.
    • System.Random, mentioned by Ievgen, already supports the range (Next(int, int)), so this is likely the easiest option.
    • If you need something better (such as not using the same seed for all instances created in the same millisecond, for example), consider Inferno's CryptoRandom.

    By the way, what's the point of randomGenerate.parmSeed(new Random().nextInt());? Your seed is based on a new instance of Random, where you use the default seed anyway, so it's not going to improve anything.

  • Community Member Profile Picture
    on at

    Hi Martin/Ievgen,

    Thanks for your reply.

    I had not tried system.random in past anytime. It is new for me. But after using same it seems it is very easy to use it. It worked for my case.

    Martin, I understood first and second approach but second seems more easy. Yes and that line of randomGenerate.parmSeed(new Random().nextInt()); is not of any use I checked.

    Regards,

    Smit Patel

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans