How-to: Generate a fixed length 6-digits random number

Hi community,
How can I generate a number using the floor method (or other) that is 6 digits in length and always random.

Thank you.

Hi @Gethino

output:

If you require additional assistance, please don’t hesitate to reach out to us.
MSquare Support
Visit us here
Youtube Channel

2 Likes

This is a better solution, since it will always be six-characters long, and is more random than the built-in random variable:

{{ substring(replace(uuid + uuid; "/\D+/g"; emptystring); 0; 6) }}

Screenshot_2024-04-17_180458

Not only that, it can begin with padded zeros:

Screenshot_2024-04-17_180443

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV to paste in the canvas.

  3. Click on each imported module and save it. You may need to remap some variables.

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 140,
                    "module": "util:ComposeTransformer",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "value": "{{substring(replace(uuid + uuid; \"/\\D+/g\"; emptystring); 0; 6)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -824,
                            "y": -3797,
                            "name": "Generate random 6-digit string",
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
4 Likes