How to Get Only Two Numbers from the random() Function?

0
Hello everyone, I'm using the random() function to generate random numbers. However, I only need to get two random numbers, so I can add them in the userID that I create.   For instance, I aim to create user IDs like UserID2222_23, UserID4332_01, UserID4882_99, where UserID remains constant, and the two appended numbers are random.   Could someone kindly advise on how to adjust the random() function to generate only two random digits each time it is called?   Any insights or suggestions would be highly appreciated. Thank you in advance for your assistance!
asked
3 answers
1

https://docs.mendix.com/appstore/partner-solutions/ats/rg-one-random-number/

 

You can set the range of the randomnumber.

After this you have to merge the two.

Not sure but I think you need to set both variables to string.

If you have to convert int to str inplace you can use toString(var).

answered
0

The built-in random() function gives you a random number >= 0.0 and < 1.0. So if you multiply this by 10 you should have a number >= 0.0 and < 10.0. If we floor() this we should get an integer between 0 and 9. We can turn this into a string, do the same operation again, and concatenate the results together. This should give you a two digit string you can then append to your UserID.

 

toString(floor(random() * 10)) + toString(floor(random() * 10))

 

https://docs.mendix.com/refguide/mathematical-function-calls/#5-random

https://docs.mendix.com/refguide/mathematical-function-calls/#6-floor

 

I hope this helps.

answered
0

use java action custom, no input, just return to string 

 

import java.util.Random;

public class GenerateRandomDigits {
    public String executeAction() throws Exception {
        Random random = new Random();

        int digit1 = random.nextInt(10); // This generates a number between 0 and 9
        int digit2 = random.nextInt(10); // This does the same for the second digit

        String result = String.format("%d%d", digit1, digit2);

        return result;
    }
}

 

answered