# Making a 12 digit random number

I want to generate a 12 digit random number that is unique everytime(important that it’s unique as it’ll reference transactions), and gets updated every second like the roll dice column. What is the easiest way to get something like this in place?

I think the best we could do is every 10 seconds unless you invest in something like this

3 Likes

Generating the number is not difficult. But I would like to understand why you want the numbers generated every second needlessly even if they are not going to be used in a transaction. Why not generate it only when you need a transaction recorded?

You may find some inspiration from @Jeff_Hager solution here. If it were me Id take Jeff’s solution and run it through a sha-256 column, strip out non-numbers with regex, and slice 12 from the last step.

2 Likes

The app was built a while ago by someone else who was using the roll dice column and then padded text to get it. We now have a separate type of transaction that occurs within the same version(?bit complicated to explain, but basically it needs a separate number from that one). TDLR; was done before that way but I’m open to a simpler way as long as it’s guaranteed to be 12 numbers long and random everytime with no repetitions.

I understand your rationale now. If it is has to be a number then you can use the `RAND()` function as explained below:

### Example:

1. RandomPart1: `ROUND(RAND() * 1000000, 0)` (produces a number like 839264)
2. RandomPart2: `ROUND(RAND() * 1000000, 0)` (produces a number like 482159)
3. Concatenate: Use a Template Column to concatenate `RandomPart1` and `RandomPart2`, resulting in a 12-digit number (e.g., `839264482159`).

You can also use a javascript column instead to do this in one-shot. For example:

``````return Math.floor(Math.random() * 1e12).toString().padStart(12, '0');
``````

However, I would encourage you to take this opportunity fix the previous broken design and use Glide’s built-in Row ID feature instead, which is a UUID (Universally Unique Identifier) that is more guaranteed to be unique.

3 Likes

Is is supposed to be numbers only, or can it contain alpha characters?

Numbers only

Well, you could use basically the same method in the post that @Eric_Penn linked to above. If you take unix time and chop off the last character, you will get 12 digits that are always unique. You just need to use the current time as a parameter, used only to trigger an new code, which will update every 10 seconds. Otherwise, pass a parameter, such as a unique ID whenever you need a new code. The parameter is not used, but it’s needed to trigger the coded to run.

``````function generateUniqueCode() {
// Get current timestamp
const timestamp = new Date().getTime().toString().substring(0,12);

return timestamp;
}

return generateUniqueCode();
``````

1 Like

Here’s another spin on it that should work pretty well.

``````function generateUniqueCode() {
// Get current timestamp
const timestamp = new Date().getTime().toString().substring(4,12);

// Generate random number
const randomPart = Math.random().toString().substring(2, 6).toUpperCase(); // You can adjust the length as needed

return randomPart + timestamp;
}

return generateUniqueCode()
``````
3 Likes