×
Namespaces

Variants
Actions
(Difference between revisions)

How to get a random number from range

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix reviewer approval and metadata)
hamishwillee (Talk | contribs)
m (Hamishwillee - Tidy wiki text)
Line 1: Line 1:
 +
[[Category:Java ME]][[Category:How To]][[Category:Code Snippet]][[Category:Base/System]]
 +
{{Abstract|This Java ME code snippet shows how to return a random number between a specified maximum and minimum value.}}
 +
 
{{ArticleMetaData <!-- v1.1 -->
 
{{ArticleMetaData <!-- v1.1 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
Line 22: Line 25:
 
|author= [[User:Firt]]
 
|author= [[User:Firt]]
 
}}
 
}}
 
 
If you need to get a random number between X and Y, you should use some mathematical pattern using the Math Class and some calculus.
 
 
You can use this quick method to get the random number:
 
  
 
<code java>
 
<code java>
Line 36: Line 34:
 
</code>
 
</code>
  
[[Category:Java ME]][[Category:How To]][[Category:Code Snippet]][[Category:Base/System]]
 
 
<!-- Translation --> [[pt:Como obter um número aleatório]]
 
<!-- Translation --> [[pt:Como obter um número aleatório]]

Revision as of 05:46, 24 January 2012

This Java ME code snippet shows how to return a random number between a specified maximum and minimum value.

Article Metadata
Article
Created: firt (15 Apr 2007)
Last edited: hamishwillee (24 Jan 2012)
public int getRandomNumber(int min, int max) {
Random rnd;
rnd = new Random();
return min + rnd.nextInt(max - min);
}
153 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×