Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overcoming MIDP limitation for Random Number Generation

 
Derek Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
john muchow
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic