Getting random numbers in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Possible Duplicate:
http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range


I would like to get a random value between 1 to 50 in Java.

How may I do that with the help of Math.random();?

How do I bound the values that Math.random() returns?

Answers

import java.util.Random;

Random rand = new Random();

int  n = rand.nextInt(50) + 1;
//50 is the maximum and the 1 is our minimum 

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/5887709/getting-random-numbers-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils