• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RANDOM NUMBER GENERATION IN MIDP

 
lexander Bosco
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ppl
am having a challenge generatin random numbers , the java.lang MATH class has no implementation of the random method()
and the java.util.Random class generates numbers in thousands rather dan in 10s.
and what is wrong with the java.util.Random nextDouble() method ? when i use it the builder tells me e.g:
i use it this way = Random rd = new Random();
double top = rd.nextDouble();

------------------------------------------------
C:\WTK22\apps\eTranzact\src\mpayment.java:2065: cannot resolve symbol
symbol : method nextDouble ()
location: class java.util.Random
double top = rd.nextDouble();
^
-----------------------------------------------

what u guys suggest?
[ August 04, 2005: Message edited by: lexander Bosco ]
 
Shawn Fitzgerald
author
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: when is the random object returning in 1000's and not 10's? Is that on the nextInt(int n) method? The 'n' value is how many bits to support in the low order values. so for values under 100 you might have to go to 128, or things under 10 you may have to go to 16, and then skip values greater than the limit you are looking for.

in the case of nextDouble, you must be using a CLDC1.0 implementation. 'double' was not supported until CLDC1.1, before that you had no option to use double or float only int and long. How to find this out, look in java.lang.Double in the J2ME javadocs, and you will see a 'since' tag that states when the component was introduced. The primatives were not introduced before the wrapper classes so checking the wrapper dates will tell you when the primative type was introduced or if it is available.

You might just need to turn on in the WTK the CLDC1.1 requirement and re-build your project.

Hope that helps.
=Shawn
 
gsd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you not just use, where x is the number you want a random number up to (eg 1000):

long x = 1000;
long t = System.currentTimeMillis();
int result = (int)(t % x);

That should give you a fairly random number 0 and 999 (?).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic