How do you use math.random to generate random ints?

后端 未结 7 886
情话喂你
情话喂你 2020-12-06 01:23

How do you use Math.random to generate random ints?

My code is:

int abc= (Math.random()*100);
System.out.println(abc);

All it print

7条回答
  •  既然无缘
    2020-12-06 01:40

    As an alternative, if there's not a specific reason to use Math.random(), use Random.nextInt():

    import java.util.Random;
    
    Random rnd = new Random();
    int abc = rnd.nextInt(100); // +1 if you want 1-100, otherwise will be 0-99.
    

提交回复
热议问题