Random No Generation

J

John E

Can someone tell me *two* different ways of randomly generating integers
between 1 and 3, which will produce *different* results even when run
simultaneously?

I've been using this (it's not very good though):

private int getRandomNumber(int max)
{
Random rand = new Random();
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}

I need to act upon the results of this generator with more random numbers,
but unfortunately it seems to produce the same results the second time
therefore losing any element of apparent randomness.

TIA.
 
D

Dario

John said:
private int getRandomNumber(int max)
{
Random rand = new Random();
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}

final Random rand = new random();
private int getRandomNumber(int max)
{
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}
 
B

Brian Palmer

John E said:
Can someone tell me *two* different ways of randomly generating integers
between 1 and 3, which will produce *different* results even when run
simultaneously?

I've been using this (it's not very good though):

private int getRandomNumber(int max)
{
Random rand = new Random();
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}

First, there's no reason to do this. The Random creators went to some
work to provide a function to provide uniform values less than an int.
Create and store a reference to a Random in the class:

Random rand = new Random();
private int getRandomNumber(int max) { return 1+rand.nextInt(3); }

What is max supposed to do?
I need to act upon the results of this generator with more random numbers,
but unfortunately it seems to produce the same results the second time
therefore losing any element of apparent randomness.

It's not clear to me exactly what you're trying to do. How do you know
it produces the same results? (Note that with only 3 output values,
I wouldn't be surprised to see similar-looking sequences).
 
F

Filip Larsen

John E wrote
Can someone tell me *two* different ways of randomly generating integers
between 1 and 3, which will produce *different* results even when run
simultaneously?

Your requirements are a bit vague, but the following field and method
snippet should do what you ask for:

public class MyClass {

private Random rng = new Random();

private int getRandomNumber(int min, int max) {
return rng.nextInt(max-min+1)+min;
}
}

Notice that 'rng' is initialized only once and then used to generate a
sequence of numbers.

I've been using this (it's not very good though):

private int getRandomNumber(int max)
{
Random rand = new Random();
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}

I need to act upon the results of this generator with more random numbers,
but unfortunately it seems to produce the same results the second time
therefore losing any element of apparent randomness.

This is not good for several reasons:

- Calling new Random() is, as the documentation says, equivalent to new
Random(System.currentTimeMillis()), which in your case means that
'rand.nextInt()' will return the exact same number until the next clock
tick.

- Do not use modulus on Random.nextInt() to get an evenly distributed
random number, use Random.nextInt(int) instead (see documentation for
the reason why).


Regards,
 
A

Andrew Thompson

John E said:
Can someone tell me *two* different ways of randomly generating integers
between 1 and 3, which will produce *different* results even when run
simultaneously?

I've been using this (it's not very good though):

private int getRandomNumber(int max)
{
Random rand = new Random();
return (int)(1 + (Math.abs(rand.nextInt()) % 3));
}

Try this,..

java.util.Random r = new Random();

private int getRandomNumber()
{
return r.nextInt(); // with whatever transforms..
}

Using a single random number generator
is the best way to produce a series of
random numbers..

HTH
 
A

Alex Hunsley

Filip said:
John E wrote




Your requirements are a bit vague, but the following field and method
snippet should do what you ask for:

public class MyClass {

private Random rng = new Random();

private int getRandomNumber(int min, int max) {
return rng.nextInt(max-min+1)+min;
}
}

Notice that 'rng' is initialized only once and then used to generate a
sequence of numbers.





This is not good for several reasons:

- Calling new Random() is, as the documentation says, equivalent to new
Random(System.currentTimeMillis()), which in your case means that
'rand.nextInt()' will return the exact same number until the next clock
tick.
Not true. Any Random objects you *initialize* in the same clock tick
will show the same deterministic sequence.
Calling nextInt will always produce the next int, no matter if the clock
has ticked forward since last time or not.
It's the initialisation which is dependent (determined by) the current
system time.
- Do not use modulus on Random.nextInt() to get an evenly distributed
random number, use Random.nextInt(int) instead (see documentation for
the reason why).
Agreed, modulus results in a less random sequence in the end.

alex
 
T

Thomas Schodt

Not true. Any Random objects you *initialize* in the same clock tick
will show the same deterministic sequence.
Calling nextInt will always produce the next int, no matter if the clock
has ticked forward since last time or not.
It's the initialisation which is dependent (determined by) the current
system time.

I think he meant 'rand.nextInt()' *with a new instance of rand every call*
will return ...
 
F

Filip Larsen

I wrote

and Alex Hunsley answered
Not true. Any Random objects you *initialize* in the same clock tick
will show the same deterministic sequence.
Calling nextInt will always produce the next int, no matter if the clock
has ticked forward since last time or not.
It's the initialisation which is dependent (determined by) the current
system time.

Yes, I am not saying otherwise.

Please note the words "in your case", by which I am refering to the code
of the orignal poster. Here, all calls to the getRandomNumber method
within same clock tick (i.e. within the period of time where
System.currentTimeMillis() return the same value) will return the same
number because 'rand' in the expression 'rand.nextInt()' refers to a new
instance.


Regards,
 
A

Alex Hunsley

Filip said:
I wrote




and Alex Hunsley answered




Yes, I am not saying otherwise.

Please note the words "in your case", by which I am refering to the code
of the orignal poster. Here, all calls to the getRandomNumber method
within same clock tick (i.e. within the period of time where
System.currentTimeMillis() return the same value) will return the same
number because 'rand' in the expression 'rand.nextInt()' refers to a new
instance.


Regards,
Oh, I see what you mean now! Ok.

alex
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top