Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

random

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

My code is:

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

All it prints out is 0, how can I fix this?

like image 818
Gworf Hi Avatar asked Dec 22 '11 22:12

Gworf Hi


People also ask

How do you generate random numbers in Java range using math random?

Random rand = new Random(); int x = rand. nextInt(10); x will be between 0-9 inclusive. So, given the following array of 25 items, the code to generate a random number between 0 (the base of the array) and array.

What is the use of the math random () function?

random() The Math. random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range.


1 Answers

Cast abc to an integer.

(int)(Math.random()*100);
like image 84
Russell Avatar answered Sep 18 '22 22:09

Russell