aspose file tools*
The moose likes Java Micro Edition and the fly likes Overcoming MIDP limitation for Random Number Generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Overcoming MIDP limitation for Random Number Generation" Watch "Overcoming MIDP limitation for Random Number Generation" New topic
Author

Overcoming MIDP limitation for Random Number Generation

Derek Smith
Greenhorn

Joined: Jan 28, 2002
Posts: 5
It has become quickly clear to me that the MIDP API has made a few changes to the java.util.Random class, particularly getting rid of the Random.nextDouble() method.
So, how would you create a pseudorandom number between x and y using only the nextInt() method? I have tried a number of varieties and none have yielded accurate results.
Thanks in advance...
DMS
sameet more
Greenhorn

Joined: Dec 19, 2001
Posts: 10
x = ((random.nextInt() >>> 1) % getWidth())
y = ((random.nextInt() >>> 1) % getHeight()
x will be a number between 0 and the width of screen
y will be a number between 0 and the height of the screen


sameet more <P>wireless java programmer
john muchow
Ranch Hand

Joined: Mar 24, 2001
Posts: 49
That last code example looks an awful lot like a code example from my book
Either way, as a further clarification, the right shift (>>>) removes the sign bit, leaving you with a positive integer. The mod (%) operator will give you a value in the range you desire.
John
Author
Core J2ME Technology and MIDP
[ January 31, 2002: Message edited by: john muchow ]
sameet more
Greenhorn

Joined: Dec 19, 2001
Posts: 10
well john muchow i do agree with u that i must have taken the code from one of the site on which u have put the code.i am still learning the tricks of j2me and hopefully we can learn a lot from ur articles on thew net as the book is still unavailable to us.Please accept my gratitude and keep contributing towards j2me.
thanks
john muchow
Ranch Hand

Joined: Mar 24, 2001
Posts: 49
sameet,
Glad to know that you've found some of the examples helpful. Keep experimenting with all the applications you can find, it's often the best way to learn.
john
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overcoming MIDP limitation for Random Number Generation