Random number Java

Generate random number Android Java example source code.

		Random rand = new Random();
		int i = rand.nextInt() % 256; // range -255  +255
		System.out.print(i + "\n"); // -184
		i = Math.abs(rand.nextInt() % 12); // range 0  +11
		System.out.print(i); // 7
// Math.random() start with 0.  e.g. 0.35981234 
int nRan = (int) (Math.random()*10); // 0 - 10
 
// nextDouble(), nextFloat(), nextInt(), nextLong() returns 0 - 10
import java.util.Random;´
 
Random r = new Random();
int nRan = r.nextInt(); // 0 - 10
double dRan = r.nextDouble() * 10; // e.g. 7.496285271597397
 
nextDouble – return 0 - 1
nextFloat – same as double
nextInt –  -2147483648  +2147483647
nextLong –  -922337203685775808  +9223372036854775807
nextGaussian –  0.0 aberation 1.0.
This entry was posted in Android Examples Code, Array,List,Collections and tagged , , , , . Bookmark the permalink.


Comments are closed.

2,347 views