DEV Community

Maxime Guilbert
Maxime Guilbert

Posted on • Edited on

Generate random values in your properties - Spring Boot

During some configurations with Kafka, I was at a point where I need a different group id for each instance of the same service.

So to set up it, I looked for how I can make prefixed group ids. And I found this.


In your application.properties or in your application.yaml, you can use the next functions to generate random values.

# Random int
a=${random.int}

# Random int with a maximum value
b=${random.int(1000)}

# Random int in a range
c=${random.int[0, 9999])} 

# Random long with a maximum value
d=${random.long(100000000000000000)}

# Random long in a range
e=${random.long[10000, 999999999999999999])}

# Random 32 bytes value
f=${random.value}

# Random UUID
g=${random.uuid}
Enter fullscreen mode Exit fullscreen mode

And if you want, you can use them in the middle of a string.

h=test-${random.int}-value
Enter fullscreen mode Exit fullscreen mode

I hope it will help you!

Don't hesitate to give some feedback to help me to improve my writing skills. Thanks!

Top comments (4)

Collapse
 
tsanyt profile image
Shrikant

Below are corrected values for range.

# Random int in a range
c=${random.int[0, 9999]} 

# Random long in a range
e=${random.long[10000, 999999999999999999]}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bzani profile image
Bruno Zani

Very interesting tip, thanks for sharing.

Collapse
 
mnmpeterson profile image
mnmpeterson

I couldn't get the range examples to work . . . even after adding the missing left parentheses.

Collapse
 
mxglt profile image
Maxime Guilbert

Did you add it in your applilcationn.yaml file?