Random rand = new Random(); float randomFloat = rand.nextFloat(); System.out.println("Random float: " + randomFloat);
Random rand = new Random(); float min = 0.5f; float max = 1.5f; float randomFloatInRange = rand.nextFloat() * (max - min) + min; System.out.println("Random float within range: " + randomFloatInRange);In both of these examples, the java.util.Random class is used to generate a random float value between 0.0 and 1.0. In the second example, the nextFloat() method is used to generate the random value and then it is scaled to the desired range.