Jump to content
  • Advertisement
Sign in to follow this  

Random Number Generator on J2ME

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heyas Just wanted to know what you guys usually use for a Random Number Generator on a J2ME midlet. I know J2ME provides the usual "(rand()%MAX_VALUE)+1;" methods but I never liked using the default randomiser functions as their arent very good. I read that the Mersenne Twister is excellent for random numbers and I was wondering if it was worth it to implement it (overkill?). I got a fast implementation of it for J2ME so I could convert it but Im not sure about performance on a mobile for this. So... what do you guys use? By the way Im looking for low numbers, your average dnd dices (d4,d6,d8,d10,d12,d20 and d100). and no.. not an rpg, just bulding ai tables and going to use something like this for it (if situation a, use table b, roll 1d20 plus modifier to decide what enemy will do). Any help would be appreciated. Yours Truly K

Share this post


Link to post
Share on other sites
Advertisement
i usualy use:

import java.util.Random;

public static Random random;

public static void initRandom() {
random = new Random();
}
public void useRandomNumber() {
nro = Math.abs(random.nextInt())//This creates the random number...
number = 500 + nro % 700;//This is how you can use it.
}



This generates a number between 500 and 1200.
CYa

Share this post


Link to post
Share on other sites
Quote:
Original post by Goguis
i usualy use:
*** Source Snippet Removed ***

This generates a number between 500 and 1200.
CYa


Yeah, thats the usual java code.
Thing is: is the Random object really good? usually the normal implementation of Random are kinda of weak. Also arent you supposed to seed that first.
By weak I mean: quickly calling the random produces the same result (used to have this in C long time ago when I first wrote my dice rolling code, over 5 years at least). This would mean that calling 5 dice rolls one after the other would produce roughly the same value for them.

Thats why I was wondering if anyone was using some other algorithm besides the Random default implementation.
So... any thoughts?

Yours truly
K

Share this post


Link to post
Share on other sites
Quote:
Original post by BloodWarrior
Thing is: is the Random object really good? usually the normal implementation of Random are kinda of weak. Also arent you supposed to seed that first.


They are weak, generally, but usually good enough for games. And yes, you're supposed to seed it, unless you want reproducible results for testing purposes. You do this by providing an argument (of type long) to the Random object constructor - e.g. 'new Random(System.currentTimeMillis())'.

Quote:
By weak I mean: quickly calling the random produces the same result (used to have this in C long time ago when I first wrote my dice rolling code, over 5 years at least). This would mean that calling 5 dice rolls one after the other would produce roughly the same value for them.


That's not the way in which they're weak, though. If it made a difference how quickly you called the function repeatedly, then you were doing something wrong - most likely, re-seeding before each random number generation. You should only seed once. (The OO model makes it easier to get this right, by having you do the seeding with a constructor and the generation with a method call.)

Quote:
Thats why I was wondering if anyone was using some other algorithm besides the Random default implementation.


It can be done, but consider that the default implementation likely takes place in native code, while anything you write will have to be done in bytecode (and AFAIK, cell phones aren't nearly as advanced as desktop machines when it comes to stuff like JIT compilation).

Share this post


Link to post
Share on other sites
Thanks for your answers guys

I had forgotten to update this and for that i apologise.

Using my desktop and running a series of random numbers by the default Random, a Mersenne Twister implementation and a Fast Mersenne Twister Implementation I found that that:

the fastest one is the default random with FastTwister really close.

other than that both the mersenne twister and the fast twister produce exactly the same results (all were initialised using the same seed).

the mersenne twister produces (on average) more repetitions of the same value (in 80 calls to nextint i ended up having often 3 results of the same digit).

Maybe my implementations of the twister werent that good but I felt like it was better to use the Default Random Class instead.

here is the reference to the mersenne twister algorithm I tested:

MersenneTwister and MersenneTwisterFast


*

Version 15, based on version MT199937(99/10/29)
* of the Mersenne Twister algorithm found at
*
* The Mersenne Twister Home Page
, with the initialization
* improved using the new 2002/1/26 initialization algorithm
* By Sean Luke, October 2004.

Yours Truly
K

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!