Please review this code

J

John B. Matthews

guptan said:
class RNG
{
private Random random;

public RNG() {
random = new Random(System.currentTimeMillis());
}
...
}

Depending on your processor speed and clock resolution, repeated use of
this constructor may result in identically seeded generators. The
no-parameter constructor already "sets the seed of the random number
generator to a value very likely to be distinct from any other
invocation of this constructor."

random = new Random();

<http://java.sun.com/javase/6/docs/api/java/util/Random.html#Random()>
 
R

Roedy Green

I was writing a non predictable random number generator for cldc1.0
. I never tried to replace any existing functionality.
Only the following methods were available to me.

The main thing that jumped out at me is it is missing the JavaDoc.
Without that, I don't even know for sure what you are TRYING to do.
--
Roedy Green Canadian Mind Products
http://mindprod.com

On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
 
G

guptan

     It works just fine if max is 1, 2, 3, 4, 6, 8, 12, 24 or one of
their negatives.  For all other values of max it produces an uneven
distribution -- yes, even for powers of two like 16 and 1024.

     (Hint: nextInt(int) is not the same as bits(int).)

ok. Please get me a reliable solution for this one assuming the
limitations of j2me cldc 1.0 platform.

public class RNG extends Random
{
public RNG(long seed) {
super(seed);
}

public int getRandom(int max){
//Your suggested code here >>>>>>>>>>>>>
}

public int getRandom(int low, int high) {
return getRandom(high - low + 1) + low;
}
}
 
L

Lew


It is not necessary and is unconventional to quote sigs.
ok. Please get me a reliable solution for this one assuming the
limitations of j2me cldc 1.0 platform.

public class RNG extends Random

You should compose rather than extend.

private final Random rand;
{
public RNG(long seed) {
super(seed);
rand = new Random( seed );
}

public int getRandom(int max){
//Your suggested code here >>>>>>>>>>>>>

Assuming you want an int from the range [0, max},

return rand.nextInt( max );
}

public int getRandom(int low, int high) {
return getRandom(high - low + 1) + low;

You'll need to range-check the arguments for this formula.
 
M

markspace

guptan said:
ok. Please get me a reliable solution for this one assuming the
limitations of j2me cldc 1.0 platform.


Why do you think the Random class is not reliable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top