Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Overcoming MIDP limitation for Random Number Generation

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic