All Articles

random ids in idea

Stop entering ad-hoc magic constants or 1,2,3,… sequences in your intent request codes, or build scripts to generate random translation keys. Embrace randomness and just use groovy scripts directly in IntelliJ live templates. A random positive integer might simply be

$RANDOM$=groovyScript("new Random().nextInt() & Integer.MAX_VALUE")

a short

$RANDOM$=groovyScript("new Random().nextInt(65535) + 1")

and a fixed 7-digit, 0-padded number

$RANDOM$=groovyScript("String.format(\"%07d\", new Random().nextInt(999999))")

The definition for the latter would be:

<template name="rnd7p0" value="$RANDOM$" description="Generate a random number up to 7 digits, left padded with 0s if needed." toReformat="false" toShortenFQNames="true">
    <variable name="RANDOM" expression="groovyScript(&amp;quot;String.format(\&amp;quot;%07d\&amp;quot;, new Random().nextInt(999999))&amp;quot;)" defaultValue="" alwaysStopAt="false" />
    <context>
      <option name="OTHER" value="true" />
    </context>
  </template>

Or, you can install the randomness plugin.