Search code examples
javastringrandomjava-11sonarlint

In Java 11, how do you get a sonarLint warning-free way of getting an index?


I'm using Java 11 and SonarLint. I have this code for getting a random index in a string (SALTCHARS is a string of length 36)

  int index = (int) (rnd.nextFloat() * SALTCHARS.length());

However, using SonarLint, this generates this warning

enter image description here

I can't simply replace "nextFloat" with "nextInt" as that functionally changes the result of this code. What is a SonarLint-safe way of generating a random index in a string?


Solution

  • int index = rnd.nextInt(SALTCHARS.length());