# Random Number Generator on J2ME

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

## 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 on other sites
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 on other sites
Quote:
 Original post by Goguisi 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 on other sites
Quote:
 Original post by BloodWarriorThing 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 on other sites

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
*
, with the initialization
* improved using the new 2002/1/26 initialization algorithm
* By Sean Luke, October 2004.

Yours Truly
K

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!