Velocity Reviews > Java > Random No Generation

# Random No Generation

John E
Guest
Posts: n/a

 12-23-2003
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.

Dario
Guest
Posts: n/a

 12-23-2003
John E wrote:

> 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));
}

Brian Palmer
Guest
Posts: n/a

 12-23-2003
"John E" <(E-Mail Removed)> writes:

> 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).

--
See comp.lang.java.announce for java-related announcements

Filip Larsen
Guest
Posts: n/a

 12-23-2003
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,
--
Filip Larsen

Andrew Thompson
Guest
Posts: n/a

 12-23-2003
"John E" <(E-Mail Removed)> wrote in message
news:bs9606\$an9bs\$(E-Mail Removed)-berlin.de...
> 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

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site

Alex Hunsley
Guest
Posts: n/a

 12-23-2003
Filip Larsen wrote:

>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.
>
>

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

Thomas Schodt
Guest
Posts: n/a

 12-23-2003
Alex Hunsley <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Filip Larsen wrote:
>>- 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.

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

Roedy Green
Guest
Posts: n/a

 12-23-2003
On Tue, 23 Dec 2003 11:58:27 +0100, Dario <(E-Mail Removed)> wrote
or quoted :

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

nextInt works even better. See
http://mindprod.com/jgloss/randomnumbers.html

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Filip Larsen
Guest
Posts: n/a

 12-23-2003
I wrote

> >- 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.

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,
--
Filip Larsen

Alex Hunsley
Guest
Posts: n/a

 12-24-2003
Filip Larsen wrote:

>I wrote
>
>
>
>>>- 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.
>>
>>

>
>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