Hi,

I want to use the Token module random number generator, but it says use [random:number] to generate "A random number from 0 to 2147483647".

How can I change this and get fixed number of digits?

For example, I need a 4 digits in each generated number (1000 to 9999). Or only 3 digits in each number (from 001 to 100).

Thank you,

Comments

C.E.A created an issue. See original summary.

berdir’s picture

Category: Support request » Feature request

You can't, there is no such option. You could always implement your own token that returns the structure you need. Changing to a feature request but I'm not convinced that this is useful enough to be added to the module.

c.e.a’s picture

It is indeed useful enough to be added to the module.

Plenty of cases where I want to add a random number somewhere for example to a node title just to increase the dificulty of the node to be easily found.

But i don't want to add a very long random number such as 28474973736378474 because just a shirt random number 78975 will do the job.

You could always implement your own token that returns the structure you need

Can you give an example on how to ? Or maybe point to a tutorial ?

Thank you,

subson’s picture

I think this can be implemented using hook_tokens() and you can define your own [random:number] definition to return 4 digit numbers.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%2...

murz’s picture

I vote for implement this feature too, it will be very useful to distribute file uploads between subfolders. By default files are stored in "YYYY-MM" folders, but this not solve the problem when most of files are uploaded in one month (at first site filling process).
So good solution will be use token like [random:number:10:99] for distribute files between subfolders.

anybody’s picture

Well I guess as feature request this is a good idea. We should stick to PHP's rand function if parameters are given:

https://www.php.net/manual/de/function.rand.php

rokzabukovec’s picture

Status: Active » Needs review
StatusFileSize
new1022 bytes

Hi.
I created a patch that makes the random:number token dynamic so that the user can provide an extra parameter of a max number. For example the user can create a 4 digit random number with creating a token [random:number:9999].
Best regards and please review.

deaom’s picture

Status: Needs review » Reviewed & tested by the community

Tested the functionality with different max number parameters and it works. So marking it as RTBC.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

This seems useful but the token description needs to be updated to explain the allowed options. Tests would be good as well, not sure if we already have random number tests.

anweshasinha’s picture

StatusFileSize
new1.1 KB

Hi,
I have changed the token description and re generated the patch. I have tested the patch in Drupal 10.0.12-dev and the patch was working properly in my system. Could you please review the patch

van.dordafog’s picture

StatusFileSize
new1.29 KB

reroll for v1.17