Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

How to get a random number from range

From Wiki
Jump to: navigation, search
hartti (Talk | contribs)
m (added a category)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
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.  
+
[[Category:General Programming on Java ME]][[Category:Code Snippet]][[Category:How To]]
 +
{{Abstract|This Java ME code snippet shows how to return a random number between a specified maximum and minimum value.}}
  
You can use this quick method to get the random number:
+
{{ArticleMetaData <!-- v1.1 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20070415
 +
|author= [[User:Firt]]
 +
}}
  
 
<code java>
 
<code java>
Line 7: Line 26:
 
   Random rnd;
 
   Random rnd;
 
   rnd = new Random();
 
   rnd = new Random();
   return min + Math.abs(rnd.nextInt()) % (max - min);
+
   return min + rnd.nextInt(max - min);
 
}
 
}
 
</code>
 
</code>
  
[[Category:Java]][[Category:Java ME]][[Category:How To]][[Category:Code Examples]][[Category:Base/System]][[Category:Level-Basic]]
+
<!-- Translation --> [[pt:Como obter um número aleatório]]

Latest revision as of 23:44, 22 July 2013

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 (22 Jul 2013)
public int getRandomNumber(int min, int max) {
Random rnd;
rnd = new Random();
return min + rnd.nextInt(max - min);
}
This page was last modified on 22 July 2013, at 23:44.
184 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.

×