Namespaces
Variants
Actions
(Difference between revisions)

Como obter um número aleatório

Jump to: navigation, search
m (Hamishwillee - Automated change of category from Lang-PT to Unlikely Category. (Moving))
Line 1: Line 1:
[[Category:Java ME (Português)]][[Category:Lang-PT]][[Category: Exemplos de código Java ME (Português)]]
+
[[Category:Java ME]][[Category:Lang-Portuguese]][[Category:Code Examples]]
  
 
Original: [[How to get a random number from range]] (Inglês)
 
Original: [[How to get a random number from range]] (Inglês)

Revision as of 08:25, 14 July 2011


Original: How to get a random number from range (Inglês)

Se você precisa obter um número aleatório entre X e Y, você deve usar algum padrão matemático usando a classe Math e alguns cálculos.

Você pode usar esse método para obter rapidamente o número aleatório:

public int getRandomNumber(int min, int max) {
Random rnd;
rnd = new Random();
return min + Math.abs(rnd.nextInt()) % (max - min);
}
124 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved